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

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

问题描述

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

假设recvfrom()在服务器端close()时应返回0,而是返回-1,错误资源暂时不可用.此资源是否引用了服务器上的已关闭套接字?还是其他完全不同的东西,例如用完缓冲区或其他东西(我认为这是不正确的)?

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

处理错误

if(SOMEMACRO)
   do something 

如何找出SOMEMACRO是什么?我打印出错误,但是它说资源临时不可用,recvfrom()描述中没有提到不合法的资源.

顺便说一句,这是一个非阻塞套接字,如果有任何区别,因为我读到如果设置了O_NONBLOCK且没有可用的消息,它将把errno设置为EAGAIN或EWOULDBLOCK.未设置O_NONBLOCK,但设置了MSG_DONTWAIT.它们与O_NONBLOCK用于通用文件描述符而MSG_DONTWAIT是特定于套接字的基本相同吗?

我的大脑现在不能很好地工作,如果有人能启发我并弄清我的困惑,我将深表感激.谢谢!

解决方案

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

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

这似乎是您正在发生的事情

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

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!

解决方案

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

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

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