Monday, December 10, 2012

Windows Socket & C++ Basic TCP Socket

TCP server/Client Basic

TCP

TCP連線

Socket-Server:

   1: #include <WinSock2.h>

   2: #include <iostream>

   3: #pragma  comment(lib,"wsock32.lib")

   4: #define BUF_SIZE  64

   5:  

   6: WSADATA wsd;

   7: SOCKET       sServer;

   8: SOCKET    sClient;

   9: int               retVal;

  10: char buf[BUF_SIZE];

  11:  

  12:  

  13: SOCKADDR_IN addrServ;

  14:  

  15: int main ()

  16: {

  17:  

  18:  

  19:     //Initialize Server

  20:     if(WSAStartup(MAKEWORD(2,2), &wsd) != 0)

  21:     {

  22:         std::cout << "WSAStartup failed!" << std::endl;

  23:         return 1;

  24:     }

  25:  

  26:     sServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

  27:     if(INVALID_SOCKET ==sServer)

  28:     {

  29:         std::cout << "Socket Fail"<<std::endl;

  30:         WSACleanup();

  31:         return -1;

  32:     }

  33:     //Bind The Socket

  34:     addrServ.sin_family = AF_INET;

  35:     addrServ.sin_port=htons(4999);

  36:     addrServ.sin_addr.s_addr = INADDR_ANY;

  37:     retVal = bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));

  38:  

  39:     if(SOCKET_ERROR == retVal)

  40:     {

  41:         std::cout << "Bind Fail " << std::endl;

  42:         closesocket(sServer);

  43:         WSACleanup();

  44:         return -1;

  45:     }

  46:     //Listen

  47:     retVal =listen(sServer,1);

  48:     if(SOCKET_ERROR == retVal)

  49:     {

  50:         std::cout<< "Listen Fail" << std::endl;

  51:         closesocket(sServer);

  52:         WSACleanup();

  53:         return -1;

  54:     }

  55:     //Accept Client Connection

  56:     sockaddr_in addrClient;

  57:     int addrClientlen = sizeof(addrClient);

  58:     sClient = accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);

  59:  

  60:     if(INVALID_SOCKET == sClient)

  61:     {

  62:         std::cout << "AcceptFAIL"<< std::endl;

  63:         closesocket(sServer);

  64:         WSACleanup();

  65:         return -1;

  66:     }

  67:     //Accept Date & print Data

  68:     ZeroMemory(buf,BUF_SIZE);

  69:     retVal = recv(sClient,buf,BUF_SIZE,0);

  70:     if(SOCKET_ERROR == retVal)

  71:     {

  72:         std ::cout << "Recv FAIL" << std::endl;

  73:         closesocket(sServer);

  74:         closesocket(sClient);

  75:         WSACleanup();

  76:         return -1;

  77:     }

  78:  

  79:     std::cout << buf << std::endl;

  80:     //Exit

  81:     closesocket(sServer);

  82:     closesocket(sClient);

  83:     WSACleanup();

  84:     return 0;

  85: }


Socket-Client:






   1: #include <WinSock2.h>

   2: #include <iostream>

   3: #pragma  comment(lib,"wsock32.lib")

   4: #define BUF_SIZE 64

   5:  

   6: WSADATA     wsd;

   7: SOCKET        sHost;

   8: SOCKADDR_IN  servAddr;

   9: char     buf[BUF_SIZE];

  10: int retVal;

  11:  

  12:  

  13:  

  14: int main()

  15: {

  16:     //Initialize Socket DLL

  17:     if (WSAStartup(MAKEWORD(2,2),&wsd) != 0)

  18:     {

  19:         std::cout << "WSAStartup" << std::endl;

  20:         return -1;

  21:     }

  22:  

  23:     sHost = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

  24:     if(INVALID_SOCKET ==sHost)

  25:     {

  26:         std::cout << "Socket Fail" << std::endl;

  27:         WSACleanup();

  28:         return -1;

  29:     }

  30:     //Connect Server

  31:     servAddr.sin_family = AF_INET;

  32:     servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

  33:     servAddr.sin_port = htons((short)4999);

  34:     int nServAddlen = sizeof(servAddr);

  35:  

  36:     retVal = connect(sHost,(LPSOCKADDR)&servAddr,sizeof(servAddr));

  37:     if(SOCKET_ERROR == retVal)

  38:     {

  39:         std::cout << "connect fail"<< std::endl;

  40:         closesocket(sHost);

  41:         WSACleanup();

  42:         return -1;

  43:     }

  44:     //Send Data

  45:     ZeroMemory(buf,BUF_SIZE);

  46:  

  47:     strcpy(buf,"MyTCP");

  48:     retVal = send(sHost,buf,strlen(buf),0);

  49:     if(SOCKET_ERROR == retVal)

  50:     {

  51:         std::cout <<"send fail" <<std::endl;

  52:         closesocket(sHost);

  53:         WSACleanup();

  54:     }

  55:     //Exit

  56:     closesocket(sHost);

  57:     WSACleanup();

  58:  

  59:     return 0;

  60: }

建立成功即關閉連線。

建立TCP連線步驟

No comments:

Post a Comment