UDP 和套接字,recvfrom() 返回 -1 和资源暂时不可用 [英] UDP and sockets, recvfrom() returning -1 and resource temporarily unavailable

查看:49
本文介绍了UDP 和套接字,recvfrom() 返回 -1 和资源暂时不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端和一个服务器与 C 中的数据报 (UDP) 通信.客户端发送 5 个消息,并在接收到消息后,服务器将消息发回.在客户端完成接收消息之前,接收和发送消息都很棒.在服务器发回所有 msgs 后,它使用 close() 终止.所以来自客户端的 recvfrom() 应该返回 0,对吧?

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?

假设 recvfrom() 应该在服务器端 close() 时返回 0,它会返回 -1,错误资源暂时不可用.此资源是否对来自服务器的已关闭套接字的引用?还是因为其他完全不同的事情,比如缓冲区用完或其他事情(我认为这不是真的)?

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)?

假设我的假设是错误的并且由于服务器终止而返回 -1,我可能应该使用

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

if(SOMEMACRO)
   do something 

我如何知道 SOMEMACRO 是什么?我打印出错误但它说资源临时不可用并且 recvfrom() 描述没有提到不可用的资源..?

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..?

顺便说一句,这是一个非阻塞套接字,如果这有什么不同,因为我读到如果设置了 O_NONBLOCK 并且没有可用的 msgs,它会将 errno 设置为 EAGAIN 或 EWOULDBLOCK.O_NONBLOCK 未设置,但 MSG_DONTWAIT 已设置.它们与 O_NONBLOCK 用于通用文件描述符而 MSG_DONTWAIT 用于特定于套接字的情况基本相同吗??

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!

推荐答案

UDP是一个无状态协议,不像TCP是面向连接的.您的接收代码不会知道发送方是否关闭了它的套接字,它只知道是否有数据等待读取.根据 Linux 上 recvfrom 的手册页:

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:

如果套接字上没有可用的消息,则接收调用会等待消息到达,除非套接字是非阻塞的(参见 fcntl(2)),在这种情况下返回值 -1 并将外部变量 errno 设置为再次.

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

请注意,资源暂时不可用"和 EAGAIN 是同一个错误,一个只是用户友好的描述与定义名称.基本上它只是告诉您您正在尝试从套接字读取并且没有要读取的数据

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

这篇关于UDP 和套接字,recvfrom() 返回 -1 和资源暂时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆