winsock 程式設計(簡單客戶&服務端通訊實現)
阿新 • • 發佈:2018-11-15
winsock 程式設計(簡單客戶&服務端通訊實現)
看到此處,相信你已經對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") //引入動態連結庫:如果這裡報錯,請在Settings -> Compilier -> Linker settings ->add "libws2_32.a" 庫(不帶引號) 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 intbind_status; 25 bind_status = bind(s_server, (SOCKADDR*)&addr_server, sizeof(SOCKADDR)); 26 if(bind_status == 0) 27 { 28 printf("bind successfully!\n"); 29 } 30 else 31 { 32 printf("bind error : fail to bind! \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 send_status; //send 狀態 42 int count = 0; //統計客戶數目 43 44 SOCKET s_client; //連線的socket 45 char buf[30]; 46 while(1) 47 { 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"); 60 printf(" 客戶 %d \n", count); 61 printf(" Port:%d\n", ntohs(addr_client.sin_port)); 62 printf(" IP:%s\n", inet_ntoa(addr_client.sin_addr));//inet_ntoa(SOCKADDR.in_addr)網路地址轉換為IP 63 64 send_status = send(s_client, "Hello, I am Server!", 40, 0); 65 if(send_status == SOCKET_ERROR) 66 { 67 printf("Send failed!"); 68 } 69 } 70 closesocket(s_client); 71 } 72 73 closesocket(s_server); //關閉socket 74 WSACleanup(); 75 76 return 0; 77 }
客戶端:初始化winsock-->建立socket-->connect-->通訊(send & recv)
1 #include <winsock2.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <windows.h> 5 #pragma comment(lib, "ws2_32.lib") //引入動態連結庫 6 #define SERVER_IP "192.168.31.102" //客戶端IP 7 8 int main() 9 { 10 WORD ws_version = MAKEWORD(2, 2); //指定Winsock version 11 WSADATA wsaData; //WSA 函式的引數 12 13 /*初始化winsock*/ 14 WSAStartup(ws_version, &wsaData); 15 16 /*socket*/ 17 SOCKET s_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 18 19 SOCKADDR_IN addr_server; 20 addr_server.sin_family = AF_INET; //協議 21 addr_server.sin_port = htons(5050); //埠 22 addr_server.sin_addr.s_addr = inet_addr(SERVER_IP); //IP:任意IP 23 24 char buf[30]; 25 26 /*Connect*/ 27 Sleep(2000); 28 int cnct_status = connect(s_client, (SOCKADDR*)&addr_server, sizeof(SOCKADDR)); 29 if(cnct_status == 0) //連線成功 30 { 31 printf("Connecting... done \n"); 32 33 int recv_status = recv(s_client, buf, 30, 0);//接受服務端訊息 34 if(recv_status == SOCKET_ERROR) 35 { 36 printf("Recv : fail to recv! \n"); 37 } 38 else 39 { 40 Sleep(1000); 41 printf("Recving... done \n"); 42 printf("Message : %s \n", buf); 43 printf("Access done!\n\n"); 44 } 45 } 46 else 47 { 48 printf("Connect : fail to connect server! \n"); 49 } 50 51 closesocket(s_client); 52 WSACleanup(); 53 54 return 0; 55 }