TurnsCoffeeIntoScripts
TurnsCoffeeIntoScripts

Reputation: 3918

recvfrom function returns -1 with no error

The title pretty much says it all. I have a receiving thread waiting for input from a client, but when nothing is read, instead of returning 0 bytes read, it returns -1 yet no errors are returned.

Any hints has to why this function would behave like that?

Thanks

EDIT:

This is the receiving code

sockaddr_in remote;
remote.sin_family = AF_UNSPEC;
remote.sin_addr.s_addr = inet_addr( _host.c_str() );
remote.sin_port = htons( atoi( _port.c_str() ) );
int remoteSize = sizeof(remote);

bytesRead = recvfrom(_os.socket, 
    (char*)buffer, 
    bufferSize, 
    0,(SOCKADDR*)&remote, &remoteSize);

_error = WSAGetLastError();

When I'm executing, bytesRead is -1 and _error is 0.

Upvotes: 2

Views: 3741

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 598001

WSAGetLastError() should not be returning 0 when recvfrom() returns SOCKET_ERROR. That would suggest you are probably doing something in between the two calls that indirectly clears WinSock's error code before you can read it.

On a side note, you do not need to fill in the sockaddr_in before calling recvfrom(). It fills in the sockaddr_in for you with information about the sender. Whatever information recvfrom() needs to perform is job is obtained from the SOCKET handle instead.

Upvotes: 2

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

One other thing you can try is calling getsockopt() with SO_ERROR to check socket-specific error (vs. last error on any of the thread's sockets).

Upvotes: 2

Related Questions