// JP opened flex table

Click to See Complete Forum and Search --> : Winsock 2 overlapped receving UDP


KamenG
March 12th, 2004, 05:25 PM
I am trying to get a piece of code to work and getting an error under specific circumstances. I am using Winsock 2.2 with UDP/IP to send and receive data. I have used the samples that Microsoft provides on MSDN and extended them. I've included the code (most of it, simplified) at the bottom.

I initialize Winsock, create a receiving socket, bind it to a local interface then I start a loop in which I call WSARecvFrom() repeatedly. If there is data everything seems to be working just fine. However, if I leave the loop to run for several minutes without getting any datagrams an error will be reported by WSARecvFrom(). If then more datagrams are sent the error goes away - for several minutes. The error is WSAENOBUFS and refers to a lack of buffer space or a full queue. The strange thing is it isn't listed in the list of errors for WSARecvFrom() but it is only in the list of WSASendTo() and nowhere in that code do I call WSASendTo().

On unsuccessful calls to WSARecvFrom() (when SOCKET_ERROR is returned) I check the error and if it is WSA_IO_PENDING like it should be when the input buffers are empty, I call WSAWaitForMultipleEvents() and wait for 1000ms for a signal. If that times out I just continue and the loop makes another call to WSARecvFrom(). I also call WSAGetOverlappedResult() to process the data when it is available. I don't exclusively use the latter (i.e. instead of WSAWaitForMultipleEvents() ) because it cannot time out - it's either infinite or nothing and I want to be able to interrupt the loop at any time.

The error is reported by the line:
-> printf("Error: %d\n", iError);
which means the error after the unsuccessful call to WSARecvFrom() was not WSA_IO_PENDING but it was WSAENOBUFS.

It seems like when the request was queued by WSARecvFrom() and I went around I just queued another one, then another one but the documentation doesn't suggest any way of popping the requests.

Any idea why this is error occurring? I would really like to understand why but if you just have a suggestion for a different implementation (as long ias it's functionally equivalent) I'd be glad to hear it. Thank you in advance.

Kamen

kuphryn
March 13th, 2004, 02:29 AM
Post the call to WSARecvFrom(). If the error is no buffer, then check the pointer to the memory buffer you pass to the API.

Kuphryn

//JP added flex table