8
votes

I have a client and a server communicating with datagrams (UDP) in C. The client sends 5 msgs and upon receiving msgs, server sends msgs back. Receiving and sending messages are great until client has finished receiving the msgs. After server sending all msgs back, it terminates using close(). so recvfrom() from client should return 0, right?

Assuming recvfrom() should return 0 upon close() from server side, it returns -1 instead, with error Resource temporarily unavailable. Is this resource reference to closed socket from server? Or is it for something else entirely different like running out of buffer or something (which i don't think is true)?

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

if(SOMEMACRO)
   do something 

How do I find out what SOMEMACRO is? I print out the error but it says resource temp unavailable and recvfrom() description doesn't mention about unavilable resource..?

Btw, this is a non blocking socket, if that makes any difference since i read that if O_NONBLOCK is set and no msgs are available, it would set errno to EAGAIN or EWOULDBLOCK. O_NONBLOCK isn't set but MSG_DONTWAIT is set. Are they basically the same thing where O_NONBLOCK is for general file descriptors and MSG_DONTWAIT is socket specific??

My brain isn't working all that great now, if someone could enlighten me and clarify what my confusion is about, i would deeply appreciate it. Thanks!

2

2 Answers

16
votes

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

If no messages are available at the socket, the receive calls wait for a message to arrive, unless the socket is nonblocking (see fcntl(2)) in which case the value -1 is returned and the external variable errno set to EAGAIN.

This seems to be what is happening for you

Edit: Note that "resource temporarily unavailable" and EAGAIN are the same error, one is just the user friendly descption vs the define name. Basically its just telling you that you are trying to read from the socket and there is no data to read

2
votes

After your have closed the socket it still lingers around for sometime. Usually about two minutes or so. To avoid this, use SO_REUSEADDR socket option.

Here are some references 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 udp_listen function:

http://www.codase.com/search/display?file=L2dlbnRvbzIvdmFyL3RtcC9yZXBvcy9jb2Rhc2UuYy9zbGlycC0xLjAuMTYvd29yay9zbGlycC0xLjAuMTYvc3JjL3VkcC5j&lang=c&off=15730+15796+