dermesser / libsocket

The ultimate socket library for C and C++, supporting TCP, UDP and Unix sockets (DGRAM and STREAM) on Linux, FreeBSD, Solaris. See also my uvco library for a refreshed version!
https://borgac.net/~lbo/doc/libsocket/
Other
797 stars 195 forks source link

"Resource temporarily unavailable" when receiving with a non blocking inet_dgram_server socket #50

Closed 1arthur1 closed 7 years ago

1arthur1 commented 7 years ago

Hi,

Here is the code:

libsocket::inet_dgram_server sock("localhost", "4000", LIBSOCKET_IPv4, SOCK_NONBLOCK);

if(sock.rcvfrom(ntwrk_buf, 1024, ntwrk_from, ntwrk_from_port) != -1)
{
    cout << "data received" << endl;
}

In the console it writes "Resource temporarily unavailable" when I launch this code. After some debugging, I found that this message is coming from check_error function called in recvfrom_inet_dgram_socket when recvfrom returns -1. But for a non blocking socket, it's not an issue, that's when we didn't receive any data.

I just deleted the call to check_error and the message is not shown anymore but I don't think that's the best thing to do. Did I miss something there to make a non blocking socket?

Thanks,

Arthur

dermesser commented 7 years ago

I think this is caused by me enabling debug_write() on errors accidentally by unconditionally setting -DVERBOSE in the CMake build file ( https://github.com/dermesser/libsocket/blame/master/CMakeLists.txt#L27)

The commit just submitted should fix this.

1arthur1 commented 7 years ago

Yes that's a way to stop showing the message but check_error will still detects an error while it's not. Anyway, thanks for the commit, I'll be able to use your library without any change ;)

dermesser commented 7 years ago

It is an error, though, and it should be reported as such. recvfrom(2) returns -1 and sets errno to EAGAIN.

1arthur1 commented 7 years ago

You are right, my bad :)