关于recvfrom()的问题
执行这条语句时,计算机等待接受数据不往下执行了吗????在双核计算机上,如果在recvfrom()后面加上printf("hhhh\n"),一运行就会有若干条“hhhh”显示。谁能解释下啊
尤其是服务端,客户端好像从没出现这种情况
#include <stdio.h> #include <Winsock2.h> #include <Windows.h> SOCKADDR_IN cln_in; SOCKADDR_IN sock_in; int cz=sizeof(SOCKADDR_IN); SOCKET ser_socket; DWORD CALLBACK New_Thread(LPVOID lpParameter); void main() { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { return; } if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { WSACleanup( ); return; } ser_socket=socket(AF_INET,SOCK_DGRAM,0); HANDLE hThread=CreateThread(NULL,0,New_Thread,0,0,0); CloseHandle(hThread); sock_in.sin_addr.S_un.S_addr=htonl(INADDR_ANY); sock_in.sin_family=AF_INET; sock_in.sin_port=htons(9527); bind(ser_socket,(SOCKADDR*)&sock_in,sizeof(SOCKADDR_IN)); char sendbuf[200]; while(1) { gets(sendbuf); sendto(ser_socket,sendbuf,200,0,(SOCKADDR*)&cln_in,cz); } closesocket(ser_socket); } DWORD CALLBACK New_Thread(LPVOID lpParameter) { char recvbuf[200]={'\0'}; char fobuf[300]; while(true) { recvfrom(ser_socket,recvbuf,200,0,(SOCKADDR*)&cln_in,&cz); printf("hhhh\n"); sprintf(fobuf,"%s say:%s",inet_ntoa(cln_in.sin_addr),recvbuf); printf("%s\n",fobuf); } };以上是代码
#include <stdio.h> #include <Winsock2.h> #include <Windows.h> SOCKET cln_socket; SOCKADDR_IN addr; SOCKADDR_IN ser_addr; int cz=sizeof(SOCKADDR_IN); DWORD CALLBACK New_Thread(LPVOID lpParameter); void main() { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ return; } /* Confirm that the WinSock DLL supports 2.2.*/ /* Note that if the DLL supports versions greater */ /* than 2.2 in addition to 2.2, it will still return */ /* 2.2 in wVersion since that is the version we */ /* requested. */ if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ WSACleanup( ); return; } cln_socket=socket(AF_INET,SOCK_DGRAM,0); //HANDLE h_thread=CreateThread(NULL,0,New_Thread,0,0,0); //CloseHandle(h_Thread); ser_addr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); ser_addr.sin_port=htons(9527); ser_addr.sin_family=AF_INET; char sendbuf[200]={'\0'}; HANDLE h_Thread=CreateThread(NULL,0,New_Thread,0,0,0); CloseHandle(h_Thread); while(true) { gets(sendbuf); sendto(cln_socket,sendbuf,200,0,(SOCKADDR*)&ser_addr,cz); } closesocket(cln_socket); } DWORD CALLBACK New_Thread(LPVOID lpParamter) { char recvbuf[200]={'\0'}; while(true) { recvfrom(cln_socket,recvbuf,200,0,(SOCKADDR*)&ser_addr,&cz); if(recvbuf[0]!='\0') { //printf("hhhh\n"); printf("%s :%s\n",inet_ntoa(ser_addr.sin_addr),recvbuf); } } }以上是客户端代码