Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recvfrom() error 10035 using non-blocking sockets

I am using ioctlsocket() function to make my socket non-blocking but when I call recvfrom(), I get the error 10035 (WSAEWOULDBLOCK).

u_long mode = 1;
ioctlsocket(newSocketIdentifier, FIONBIO, &mode);

while(1)
   {
      if((recv_len = recvfrom(newSocketIdentifier, receiveBuffer, sizeof(receiveBuffer), 0, (struct sockaddr *) &clientSocket, &clientSocketLength)) == SOCKET_ERROR)
      {
         char err[128];
         itoa(WSAGetLastError(),err,10);
         MessageBox( NULL,"Could not Receive Data",err,MB_ICONINFORMATION);
         BREAK;
      }
   }

Can anybody explain why this happens? :(

like image 643
Ayse Avatar asked Jun 12 '13 11:06

Ayse


1 Answers

This is normal if no data is available. The code is WSAEWOULDBLOCK (see this table) and means, that on a blocking port the function would have to sit and wait until it could be served.

   while(1)
   {
      if((recv_len = recvfrom(newSocketIdentifier, receiveBuffer, sizeof(receiveBuffer), 0, (struct sockaddr *) &clientSocket, &clientSocketLength)) == SOCKET_ERROR)
      { 
         int ierr= WSAGetLastError();
         if (ierr==WSAEWOULDBLOCK) {  // currently no data available
             Sleep(50);  // wait and try again
             continue; 
         }

         // Other errors
         char err[128];
         itoa(ierr,err,10);
         MessageBox( NULL,"Could not Receive Data",err,MB_ICONINFORMATION);
         break;
      }
   }
like image 172
Grezgory Avatar answered Nov 05 '22 05:11

Grezgory