winsock 編程(簡單客戶&服務端通信實現)
雙向通信:Client send message to Server, and if Server receive the message, return a string "你好,客戶端!我是服務器。"
修改說明:If you want achieve the function that Server can also send message to Client, you only need to change the 70,71 and 72 line. That is, you need to add the scanf code and transform it to const char*, then send it to Client!
看到此處,相信你已經對winsock編程有了一定的了解,下面給出 簡單的實現。
其中用到的 各個函數詳細說明參考
服務端:初始化winsock-->建立socket-->bind(端口&IP&協議)-->listen-->accept*(不斷監聽&接受連接)
1 /*注意頭文件順序*/ 2 #include <winsock2.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #pragma comment(lib, "ws2_32.lib") //引入動態鏈接庫 6 7 int main() 8 { 9 WORD ws_version = MAKEWORD(2, 2); //指定Winsock version 10 WSADATA wsaData; //WSA 函數的參數 11 12 /*初始化winsock*/ 13 WSAStartup(ws_version, &wsaData); 14 15 /*socket*/ 16 SOCKET s_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 17 18 SOCKADDR_IN addr_server; 19 addr_server.sin_family = AF_INET; //協議 20 addr_server.sin_port = htons(5050); //端口 21 addr_server.sin_addr.s_addr = htonl(INADDR_ANY); //IP:任意IP 22 23 /*bind*/ 24 int bind_status; 25 bind_status = bind(s_server, (SOCKADDR*)&addr_server, sizeof(SOCKADDR)); 26 if(bind_status == SOCKET_ERROR) 27 { 28 printf("bind error : fail to bind! \n"); 29 } 30 else 31 { 32 printf("bind successfully!\n"); 33 } 34 35 /*listen*/ 36 listen(s_server, 5);//max=5 37 printf("listening ... \n"); 38 39 SOCKADDR_IN addr_client; //存儲client地址信息 40 int len = sizeof(SOCKADDR); 41 int count = 0; //統計客戶數目 42 SOCKET s_client; //連接的socket 43 44 char buf[1000]; 45 while(true) 46 { 47 printf("\n等待連接...\n"); 48 /*accept*/ 49 s_client = accept(s_server, (SOCKADDR*)&addr_client, &len); 50 if(s_client == INVALID_SOCKET) 51 { 52 printf("Accept error : fail to accept client! "); 53 } 54 else//連接成功 55 { 56 count++; 57 printf("\nAccept successfully!\n"); 58 59 printf(" 編號:%d \n", count); 60 printf(" Port:%d\n", ntohs(addr_client.sin_port)); 61 printf(" IP:%s\n", inet_ntoa(addr_client.sin_addr));//inet_ntoa(SOCKADDR.in_addr)網絡地址轉換為IP 62 63 int recv_status = recv(s_client, buf, 100, 0); 64 if(recv_status > 0) 65 { 66 printf("收到:"); 67 buf[recv_status] = 0x00;//在字符串結尾填上 結束符 0x00 == /0 參考回答 68 printf(buf); 69 } 70 const char *sendData = "你好!客戶端!我是服務器"; 71 send(s_client, sendData, strlen(sendData), 0); 72 closesocket(s_client); 73 } 74 } 75 76 closesocket(s_server); //關閉socket 77 WSACleanup(); 78 79 return 0; 80 }
客戶端:初始化winsock-->建立socket-->connect-->通信(send & recv)
1 #include <winsock2.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <windows.h> 5 #include <iostream> 6 #pragma comment(lib, "ws2_32.lib") //引入動態鏈接庫 7 #define SERVER_IP "192.168.43.100" //客戶端IP 8 using namespace std; 9 10 int main() 11 { 12 WORD ws_version = MAKEWORD(2, 2); //指定Winsock version 13 WSADATA wsaData; //WSA 函數的參數 14 15 /*初始化winsock*/ 16 WSAStartup(ws_version, &wsaData); 17 18 while(true) 19 { 20 /*socket*/ 21 SOCKET s_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 22 23 SOCKADDR_IN addr_server; 24 addr_server.sin_family = AF_INET; //協議 25 addr_server.sin_port = htons(5050); //端口 26 addr_server.sin_addr.s_addr = inet_addr(SERVER_IP); 27 28 char buf[100]; 29 int send_status, recv_status; 30 31 /*Connect*/ 32 int cnct_status = connect(s_client, (SOCKADDR*)&addr_server, sizeof(SOCKADDR)); 33 if(cnct_status == 0)//連接成功 34 { 35 printf("\nConnecting... done\n"); 36 37 scanf("%s", buf); 38 //向服務端發送消息 39 send_status = send(s_client, buf, 10, 0); 40 if(send_status == SOCKET_ERROR)//發送失敗 41 { 42 printf("send error!\n"); 43 } 44 else 45 { 46 printf("發送:%s\n", buf); 47 //接受服務端消息 48 recv_status = recv(s_client, buf, 100, 0); 49 buf[recv_status] = 0x00;//在字符串結尾填上 結束符 0x00 == /0 參考回答
50 printf("收到:%s\n", buf); 51 } 52 } 53 else 54 { 55 printf("Test:fail to connect server! \n"); 56 } 57 closesocket(s_client); 58 } 59 60 WSACleanup(); 61 62 return 0; 63 }
