Windows Sockets
-
Hi, I'm experiencing some problems with reading data from sockets. I have a thread that constanly reads(polls) data from the sockets port. Except it randomly hangs on on the recv() function. Is there something I'm doing wrong. Here is code sample. Thanks DWORD ThreadProc(LPVOID lparam) { char szBuffer[256]; while(1) { if(recv(socket, szBuffer, 256, 0)!= SOCKET_ERROR) ParseData(szBuffer); } return 0; }
-
Hi, I'm experiencing some problems with reading data from sockets. I have a thread that constanly reads(polls) data from the sockets port. Except it randomly hangs on on the recv() function. Is there something I'm doing wrong. Here is code sample. Thanks DWORD ThreadProc(LPVOID lparam) { char szBuffer[256]; while(1) { if(recv(socket, szBuffer, 256, 0)!= SOCKET_ERROR) ParseData(szBuffer); } return 0; }
do you mean the thread gets blocked on the recv() function if no data ? if yes, that's the expected behaviour: If no incoming data is available at the socket, the recv call blocks and waits for data to arrive according to the blocking rules defined for WSARecv with the MSG_PARTIAL flag not set unless the socket is nonblocking. In this case, a value of SOCKET_ERROR is returned with the error code set to WSAEWOULDBLOCK. The select, WSAAsyncSelect, or WSAEventSelect functions can be used to determine when more data arrives. Serge