为什么无论发送多少长度的数据,者能接收前四位呢?
程序代码:
// : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "WinSock2.h" #define MAX_BUFFER 100 int _tmain(int argc, _TCHAR* argv[]) { WORD wVersionRequested; WSADATA wsaData; int len; SOCKADDR_IN sockSerAddr,sockClientAddr; char *sendbuf,*recvbuf; wVersionRequested=MAKEWORD(2,2); if(0!=WSAStartup(wVersionRequested,&wsaData)) { printf("faile to connect!\n"); return 0; } if(HIBYTE(wsaData.wVersion)!=2||LOBYTE(wsaData.wVersion)!=2) { WSACleanup(); return 0; } SOCKET sockSer=socket(AF_INET,SOCK_STREAM,0); sockSerAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY); sockSerAddr.sin_family=AF_INET; sockSerAddr.sin_port=htons(3000); if(SOCKET_ERROR==bind(sockSer,(LPSOCKADDR)&sockSerAddr,sizeof(sockaddr_in))) { printf("faile to bind!\n"); return 0; } if(listen(sockSer,5) ==SOCKET_ERROR) { printf("Listen failed:%d\n", WSAGetLastError()); return 0; } len=sizeof(sockaddr); sendbuf=(char*)malloc(MAX_BUFFER*sizeof(char)); recvbuf=(char*)malloc(MAX_BUFFER*sizeof(char)); sendbuf="welcome to china !"; while(1) { SOCKET sockconnet=accept(sockSer,(sockaddr*)&sockClientAddr,&len); if(sockconnet== SOCKET_ERROR) { printf("Accept failed:%d", WSAGetLastError()); break; } printf("sockClient IP is: %s\n",inet_ntoa(sockClientAddr.sin_addr)); int len1=send(sockconnet,sendbuf,strlen(sendbuf)+1,0); if(SOCKET_ERROR==len1) { printf("send failed!\n"); break; } else printf("发送的数据长度是:%d\n",len1); memset(recvbuf,'\0',sizeof(recvbuf)); int len2=recv(sockconnet,recvbuf,sizeof(recvbuf),0); if(SOCKET_ERROR==len2) { printf("receive error!\n"); break; } else printf("接收的数据长度是:%d\n接收的数据是%s\n",len2,recvbuf); closesocket(sockconnet); } closesocket(sockSer); WSACleanup(); system("pause"); return 0; }
这是客服程序
程序代码:
// : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "WinSock2.h" #define MAX_BUFFER 100 int _tmain(int argc, _TCHAR* argv[]) { WORD wVersionRequested; WSADATA wsaData; int result; SOCKADDR_IN sockSer; char *sendbuf,*recvbuf; wVersionRequested=MAKEWORD(2,2); if(0!=WSAStartup(wVersionRequested,&wsaData)) { printf("failed to load WinSock!\n"); return 0; } if(HIBYTE(wsaData.wVersion)!=2||LOBYTE(wsaData.wVersion)!=2) { printf("Version Wrong!\n"); WSACleanup(); return 0; } SOCKET socketClient=socket(AF_INET,SOCK_STREAM,0); if(SOCKET_ERROR == socketClient) { printf("Socket() error1:%d\n", WSAGetLastError()); return 0; } sockSer.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); sockSer.sin_family=AF_INET; sockSer.sin_port=htons(3000); recvbuf=(char*)malloc(MAX_BUFFER*sizeof(char)); sendbuf=(char*)malloc(MAX_BUFFER*sizeof(char)); sendbuf="thank you very much !"; if(connect(socketClient,(LPSOCKADDR)&sockSer,sizeof(sockaddr_in))) { printf("Socket() error2:%d\n", WSAGetLastError()); return 0; } else { memset(recvbuf,'\0',sizeof(recvbuf)); int len1=recv(socketClient,recvbuf,sizeof(recvbuf),0); if(SOCKET_ERROR==len1) printf("receive error!\n"); else printf("接收到的数据长度是:%d\n收到的数据是:%s\n",len1,recvbuf); int len2=send(socketClient,sendbuf,strlen(sendbuf)+1,0); if(SOCKET_ERROR==len2) printf("send error!\n"); else printf("发送的长度是:%d\n",len2); } closesocket(socketClient); WSACleanup(); return 0; }