Brad77
December 3rd, 2009, 11:40 AM
I'm using a socket to receive data from a device.
I've created the socket, bound the required port to it, used ioctlsocket with FIONBIO to set it as non-blocking, set it listening.
Then my main loop attempts to accept a socket and uses ioctlsocket with FIONREAD to determine the number of bytes available to read.
The problem I'm having is that if there are no connections queued then the call to accept returns INVALID_SOCKET (as I'd expect) but calling WSAGetLastError returns 0 (no error occurred) I'd expect it to return WSAEWOULDBLOCK (this is what all documentation I've found has said)
I don't want to use blocking mode, as I'd like to be able to close the thread when I need to without it being stuck waiting to accept a connection.
But in non-blocking mode if after a call to accept, the LastError is not WSAEWOULDBLOCK then I've been treating it as an error (logging the error, closing the socket which is recreated a minute or so later on the next connection attempt).
I could just treat WSAGetLastError of 0 as not an error, but the documentation says that in this case the Error should be WSAEWOULDBLOCK.
So basically I can fix the problem, but it isn't doing what I'd expect according to the docs, and last week the WSAGetLastError wasn't returning zero in this case, this week it is and I don't know what I've changed (I've been working on other unrelated classes).
If anyone has any ideas on what might have changed or maybe I've misunderstood something, please let me know.
Thanks,
Brad77.
I've created the socket, bound the required port to it, used ioctlsocket with FIONBIO to set it as non-blocking, set it listening.
Then my main loop attempts to accept a socket and uses ioctlsocket with FIONREAD to determine the number of bytes available to read.
The problem I'm having is that if there are no connections queued then the call to accept returns INVALID_SOCKET (as I'd expect) but calling WSAGetLastError returns 0 (no error occurred) I'd expect it to return WSAEWOULDBLOCK (this is what all documentation I've found has said)
I don't want to use blocking mode, as I'd like to be able to close the thread when I need to without it being stuck waiting to accept a connection.
But in non-blocking mode if after a call to accept, the LastError is not WSAEWOULDBLOCK then I've been treating it as an error (logging the error, closing the socket which is recreated a minute or so later on the next connection attempt).
I could just treat WSAGetLastError of 0 as not an error, but the documentation says that in this case the Error should be WSAEWOULDBLOCK.
So basically I can fix the problem, but it isn't doing what I'd expect according to the docs, and last week the WSAGetLastError wasn't returning zero in this case, this week it is and I don't know what I've changed (I've been working on other unrelated classes).
If anyone has any ideas on what might have changed or maybe I've misunderstood something, please let me know.
Thanks,
Brad77.