简体   繁体   English

UDP和套接字,recvfrom()返回-1,资源暂时不可用

[英]UDP and sockets, recvfrom() returning -1 and resource temporarily unavailable

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. 我有一个客户端和服务器与C中的数据报(UDP)通信。客户端发送5个消息,并在收到消息后,服务器发回消息。 Receiving and sending messages are great until client has finished receiving the msgs. 在客户端完成接收消息之前,接收和发送消息是很好的。 After server sending all msgs back, it terminates using close(). 服务器发回所有消息后,它将使用close()终止。 so recvfrom() from client should return 0, right? 所以来自客户端的recvfrom()应该返回0,对吗?

Assuming recvfrom() should return 0 upon close() from server side, it returns -1 instead, with error Resource temporarily unavailable. 假设recvfrom()应该在服务器端close()时返回0,它返回-1,而错误资源暂时不可用。 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 假设我的假设是错误的,并且因为服务器终止而返回-1,我可能应该处理错误

if(SOMEMACRO)
   do something 

How do I find out what SOMEMACRO is? 我怎样才能知道SOMEMACRO是什么? I print out the error but it says resource temp unavailable and recvfrom() description doesn't mention about unavilable resource..? 我打印出错误,但它说资源临时不可用,recvfrom()描述没有提到无法使用的资源..?

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并且没有可用的消息,它会将errno设置为EAGAIN或EWOULDBLOCK。 O_NONBLOCK isn't set but MSG_DONTWAIT is set. 未设置O_NONBLOCK但设置了MSG_DONTWAIT。 Are they basically the same thing where O_NONBLOCK is for general file descriptors and MSG_DONTWAIT is socket specific?? 它们基本上是相同的东西,其中O_NONBLOCK用于一般文件描述符,而MSG_DONTWAIT是特定于套接字的吗?

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

UDP is a stateless protocol, unlike TCP which is connection oriented. UDP是一种无状态协议,与面向连接的TCP不同。 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: 根据Linux上recvfrom的手册页:

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. 如果套接字上没有可用的消息,则接收调用等待消息到达,除非套接字是非阻塞的(请参阅fcntl(2)),在这种情况下返回值-1并将外部变量errno设置为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. 编辑:请注意,“资源暂时不可用”和EAGAIN是相同的错误,一个是用户友好的描述与定义名称。 Basically its just telling you that you are trying to read from the socket and there is no data to read 基本上它只是告诉你,你正试图从套接字读取,没有数据可读

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. 要避免这种情况,请使用SO_REUSEADDR套接字选项。

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 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: 这是一个例子,向下滚动到udp_listen函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM