dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Why do I get WSAEWOULDBLOCK when calling recv()?

  1. #1
    Join Date
    Jun 2003
    Posts
    67

    Why do I get WSAEWOULDBLOCK when calling recv()?

    I have a socket connected (non-blocking, using select() for notification) and receiving all the time a large amount of data (video data). But at a certain time (randomly), I get WSAEWOULDBLOCK error. I've read out there that it means this operation is temporaly unavailable.

    My question is why? why do I receive this error when I'm always receving data? May it be due to low resources of my computer?

    And when getting this error, how should I handle it?? must I ignore and call again recv? or should I understund it as the comunication is broken and I have to reconnect the socket??

    please, help me in this issue!!!

    thanks in advance.

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Why do I get WSAEWOULDBLOCK when calling recv()?

    In general when you encounter this error, retry later for the socket is busy.

    Kuphryn

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Why do I get WSAEWOULDBLOCK when calling recv()?

    Yes, WSAEWOULDBLOCK is a normal response when using non-blocking sockets. It means that the requested receive operation was not performed, because if it had been performed, then the socket would block, which is inconsistent with the non-blocking status of the socket.

    In response to WSAEWOULDBLOCK, simply call recv again. If you're using Windows and you have set FD_READ into a WSAAsyncSelect call, then you should get a Windows message when the socket has become ready to receive, and your call to recv can be placed in the message handler (you should still be safe and check for WSAEWOULDBLOCK in the handler, even though the socket has signalled its readiness to receive).

    Mike

  4. #4
    Join Date
    Jun 2003
    Posts
    67

    Re: Why do I get WSAEWOULDBLOCK when calling recv()?

    Well guys, thank you very much. This is what I thought about that "error", but I wanted a feedback of more experienced developers.

  5. #5
    Join Date
    Apr 2001
    Location
    UK (South)
    Posts
    514

    Re: Why do I get WSAEWOULDBLOCK when calling recv()?

    What is said amongst the other posts is correct, in that you will receive the error WSAEWOULDBLOCK if you try to call recv() when there is no data available on the socket. However, you state in the original post that you are using select() to wait for notification of when data is available to read. That being the case you should not get the error.

    This would indicate to me that something is wrong in the logic of your socket handling. If you would like your code to be checked for errors then please post the relevant code section(s).

  6. #6
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Why do I get WSAEWOULDBLOCK when calling recv()?

    Interesting, I didn't know that. Thanks for this completely correct observation. Here's what the Winsock 2 (revision 2.2.2) manual says about select():
    Quote Originally Posted by Winsock 2 API
    The parameter readfds identifies those sockets which are to be checked for readability. If the socket is currently listen()ing, it will be marked as readable if an incoming connection request has been received, so that an accept() is guaranteed to complete without blocking. For other sockets, readability means that queued data is available for reading so that a recv() or recvfrom(), WSARecv() or WSARecvFrom(),is guaranteed not to block.

  7. #7
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    select() Bug in Win95 Returns WSAEWOULDBLOCK send() or recv()

    I just came across the following KB article at MSDN: "BUG: Select() Fails to Block on a Blocking Socket" at http://support.microsoft.com/kb/q177346/ . According to the article:
    The Winsock select() API might fail to block on a nonblocking socket and return WSAEWOULDBLOCK as an error code when either send() or recv() is subsequently called. For example, select() may return indicating there is data to read, yet a call to recv() returns with the error code WSAEWOULDBLOCK, indicating there is no data immediately available.
    It seems to be a bug on Win95 only, but maybe it's part of the behavior being observed by the OP.

    Mike

  8. #8
    Join Date
    Apr 2001
    Location
    UK (South)
    Posts
    514

    Re: Why do I get WSAEWOULDBLOCK when calling recv()?

    Hi MikeAThon,

    You might be right there.

    If it is the case, that the select() call is returning when there isn't data to read, then the simplest thing to do is just ignore the WSAEWOULDBLOCK and loop back round to the top and call select() again.

    Hopefully these sort of 'bugs' only occur very occasionally and won't keep it looping as fast as the CPU will go.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)