UDP and sockets, recvfrom () return -1 and the resource is temporarily unavailable

I am new to programming and network programming, so if this is stupid, not too bash, please, thanks.

I have a client and a server that interact with charts (UDP) in C. client sends 5 msgs, and when I receive messages, the server sends messages back. receiving and sending messages is great until the client finishes receiving messages. after the server sends all messages back, it exits using the close () function. so recvfrom () from the client should return 0, right?

Assuming that recvfrom () should return 0 when closing () on the server side, instead it returns -1, and the resource error is temporarily unavailable. Is this a resource link for a private socket from the server? or is it for something else, completely different, like running from a buffer or something (that I don’t think is true)?

and assuming my assumption was wrong and -1 is returned because the server is finished, I probably should handle the error with

if(SOMEMACRO) do something 

but how do you know what is SOMEMACRO ? I print an error, but it says that the temp resource is unavailable and recvfrom () in the description does not mention an unusable resource ..?

btw is a non-blocking socket, if that makes any difference, as I read that if O_NONBLOCK is set and no msgs are available, it will set errno to EAGAIN or EWOULDBLOCK. O_NONBLOCK is not installed, but MSG_DONTWAIT is installed. is it basically the same as O_NONBLOCK for shared file descriptors, and is MSG_DONTWAIT a specific socket ??

My brain is not working so well right now, if someone can enlighten me and find out what my confusion is, I would really appreciate it. Thanks!

+7
c udp sockets client
source share
2 answers

UDP is a stateless protocol, unlike TCP, which is connection oriented. Your receive code will not know if the socket has closed it or not, it only knows if there is data waiting to be read. According to the man page for recvfrom on Linux:

If there are no messages in the socket, receive calls wait for a message if the socket is not blocked (see fcntl (2)), in which case the value -1 is returned, and the external variable errno is set to EAGAIN.

This is similar to what is happening for you.

Edit: note that “the resource is temporarily unavailable” and EAGAIN is the same error, one of them is simply a user-friendly solution compared to the definition name. Basically it just tells you what you are trying to read from a socket and there is no data to read

+13
source share

After you close the socket, it still lingers for a while. Usually about two minutes or so. To avoid this, use the SO_REUSEADDR option.

Here are some links for you.

http://msdn.microsoft.com/en-us/library/ms740476%28VS.85%29.aspx http://docs.hp.com/en/B2355-90136/ch03s01.html

And here is an example, scroll down to the udp_listen function:

http: //www.codase.com

+2
source share

All Articles