views:

785

answers:

2

Hi, I'm pretty new at programming and esp network programming so if it's stupid, don't bash too hard please, thanks.

I have client and server communicating with diagrams (UDP) in C. 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)?

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

if(SOMEMACRO)
   do something 

but 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!

+1  A: 

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+

Vlad
+4  A: 

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

bdk
So if you are using UDP, would recvfrom() ever return 0? since you never know if the peer has performed an orderly shutdown?
Fantastic Fourier
@Fantastic Fourier - UDP can actually send a datagram which is just the IP and UDP headers but no data payload. It is perfectly legal and will look to you like a read of 0 bytes.
Duck
@Fantastic Fourier - Strictly speaking since there is no "connection" in UDP there is nothing to shut down, orderly or otherwise.
Duck
Thanks I think I have a better understanding of UDP now.
Fantastic Fourier