下面是自己开始学习windows socket时,写的demo。但程序运行结果服务器端结果不对?大家看下程序哪里有问题。谢谢!!下面是服务器端
1 #include <winsock2.h> 2 #include <ws2tcpip.h> 3 #include <stdio.h> 4 5 #pragma comment(lib, "ws2_32.lib") 6 #define DEFAULT_PORT 27015 7 #define DEFAULT_LISTEN_NUMBER 100 8 #define DEFAULT_BUFFER 100 9 10 int main() 11 { 12 13 WSADATA wsaData; 14 int iResult = 0; 15 struct sockaddr_in serverAddr; 16 struct sockaddr_in clientAddr; 17 SOCKET listenSocket; 18 SOCKET newConnection; 19 int clientAddrLen = 0; 20 char recvBuf[DEFAULT_BUFFER]; 21 char sendBuf[DEFAULT_BUFFER] = "server"; 22 int left = DEFAULT_BUFFER; 23 int idx = 0; 24 25 iResult = WSAStartup(MAKEWORD(2,2), &wsaData); 26 if (iResult != 0) 27 { 28 printf("WSAStartup failed with error %d\n", iResult); 29 } 30 31 listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 32 33 serverAddr.sin_family = AF_INET; 34 serverAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); 35 serverAddr.sin_port = htons(DEFAULT_PORT); 36 37 iResult = bind(listenSocket, (SOCKADDR *)&serverAddr, sizeof(serverAddr)); 38 if (iResult == SOCKET_ERROR) 39 { 40 printf("bind failed with error %d\n", iResult); 41 } 42 43 iResult = listen(listenSocket, DEFAULT_LISTEN_NUMBER); 44 if (iResult == SOCKET_ERROR) 45 { 46 printf("listen failed with error %d\n", iResult); 47 } 48 49 clientAddrLen = sizeof(clientAddr); 50 newConnection = accept(listenSocket, (SOCKADDR *)&clientAddr, &clientAddrLen); 51 52 iResult = recv(listenSocket, recvBuf, DEFAULT_BUFFER, 0); 53 if (iResult == SOCKET_ERROR) 54 { 55 printf("recv failed with error %d\n", iResult); 56 } 57 58 printf("The client has send: %s\n", recvBuf); 59 60 iResult = send(newConnection, sendBuf, strlen(sendBuf) + 1, 0); 61 if (iResult == SOCKET_ERROR) 62 { 63 printf("send failed with error %d\n", iResult); 64 } 65 66 67 closesocket(newConnection); 68 closesocket(listenSocket); 69 70 if (WSACleanup() == SOCKET_ERROR) 71 { 72 printf("WSACleanup failed with error %d\n", WSAGetLastError()); 73 } 74 75 return 0; 76 }
下面是客户端
1 #include <winsock2.h> 2 #include <stdio.h> 3 4 #pragma comment(lib, "ws2_32.lib") 5 #define DEFAULT_PORT 27015 6 #define DEFAULT_BUFFER 100 7 8 int main() 9 { 10 WSADATA wsaData; 11 SOCKET s; 12 struct sockaddr_in serverAddr; 13 int iResult = 0; 14 char sendBuf[DEFAULT_BUFFER] = "client"; 15 char recvBuf[DEFAULT_BUFFER]; 16 int left = DEFAULT_BUFFER; 17 int idx = 0; 18 19 iResult = WSAStartup(MAKEWORD(2,2), &wsaData); 20 if (iResult != 0) 21 { 22 printf("WSAStartup failed with error %d\n", iResult); 23 } 24 25 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 26 27 serverAddr.sin_family = AF_INET; 28 serverAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); 29 serverAddr.sin_port = htons(DEFAULT_PORT); 30 31 iResult = connect(s, (SOCKADDR *)&serverAddr, sizeof(serverAddr)); 32 if (iResult != 0) 33 { 34 printf("connect failed with error %d\n", iResult); 35 } 36 37 iResult = send(s, "Hello world!", 13, 0); 38 if (iResult == SOCKET_ERROR) 39 { 40 printf("send failed with error %d\n", iResult); 41 } 42 43 iResult = recv(s, recvBuf, DEFAULT_BUFFER, 0); 44 if (iResult == SOCKET_ERROR) 45 { 46 printf("recv failed with error %d\n", iResult); 47 } 48 printf("server has send: %s\n", recvBuf); 49 50 closesocket(s); 51 WSACleanup(); 52 53 return 0; 54 }
下面是运行结果:
服务器端结果:
recv failed with error -1
The client has send: 烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫
烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫
Press any key to continue
客户端结果:
server has send: server
Press any key to continue
自己找到错了
服务器端52行
iResult = recv(listenSocket, recvBuf, DEFAULT_BUFFER, 0);应该是
iResult = recv(newConnection , recvBuf, DEFAULT_BUFFER, 0);