Discussion:
Using AsyncSocket how can I catch SOCKET_ERROR?
(too old to reply)
Trung Nguyen
2016-05-11 02:52:19 UTC
Permalink
I'm new with MFC and AsyncSocket. Can someone kindly tell me how to catch socket operations' error (like connect(), send(), or recv()...)? Because when I do this:

int iLen = recv(connectSocket, socketBuffer, sizeToRead , NULL);
if(iLen==SOCKET_ERROR) {
//handle error here
} else {
//do something with the received data
}

It always return error, but when I remove the check, the socket actually received data.
R.Wieser
2016-05-11 08:46:20 UTC
Permalink
Trung Nguyen,

When you get a SOCKET_ERROR status back you need to call WSAGetLastError to
get *specifics* about what happened. And not all of the than returned
"errors" are actually *errors* (but just a kind of heads-up/status).

One such a possible result is WSAEWOULDBLOCK (10035 decimal), which tells
you the data-retrieval is pending.

For more info see (for instance)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms737625.aspx

Regards,
Rudy Wieser
Post by Trung Nguyen
I'm new with MFC and AsyncSocket. Can someone kindly tell me how to catch
socket operations' error (like connect(), send(), or recv()...)? Because
Post by Trung Nguyen
int iLen = recv(connectSocket, socketBuffer, sizeToRead , NULL);
if(iLen==SOCKET_ERROR) {
file://handle error here
} else {
file://do something with the received data
}
It always return error, but when I remove the check, the socket actually received data.
Loading...