1 //我们假设客户端的代码如下(为代码直观,省去所有错误检查,以下同): 2 #include <WINSOCK2.H> 3 #include <stdio.h> 4 #define SERVER_ADDRESS "137.117.2.148" 5 #define PORT 5150 6 #define MSGSIZE 1024 7 #pragma comment(lib, "ws2_32.lib") 8 int main() 9 { 10 WSADATA wsaData; 11 SOCKET sClient; 12 SOCKADDR_IN server; 13 char szMessage[MSGSIZE]; 14 int ret; 15 16 // Initialize Windows socket library 17 WSAStartup(0x0202, &wsaData); 18 // Create client socket 19 sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 20 // Connect to server 21 memset(&server, 0, sizeof(SOCKADDR_IN)); 22 server.sin_family = AF_INET; 23 server.sin_addr.S_un.S_addr = inet_addr(SERVER_ADDRESS); 24 server.sin_port = htons(PORT); 25 connect(sClient, (struct sockaddr *)&server, sizeof(SOCKADDR_IN)); 26 while (TRUE) 27 { 28 printf("Send:"); 29 gets(szMessage); 30 // Send message 31 send(sClient, szMessage, strlen(szMessage), 0); 32 // Receive message 33 ret = recv(sClient, szMessage, MSGSIZE, 0); 34 szMessage[ret] = '