第一范文网 - 专业文章范例文档资料分享平台

网络编程Socket之UDP(三)超时设置和非阻塞

来源:用户分享 时间:2025/6/6 22:20:35 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

网络编程Socket之UDP(三)超时设置和非阻塞

前面遗留的两个问题:

1.一个已连接UDP套接字能且仅能与一个对端交换数据报,那么客户端发送广播的时候如何防止recvfrom方法阻塞; 2.服务端忙的时候,已连接的UDP套接字也会被阻塞。 方法一:设置超时 UNP 14.2

There are three ways to place a timeout on an I/O operation involving a socket:

1.Callalarm, which generates theSIGALRMsignal when the specified time hasexpired. This involves signal handling, which can differ from oneimplementation to the next, and it may interfere with other existing calls toalarmin the process.2.Block waiting for I/O inselect, which has a time limit built-in, instead ofblocking in a call toread orwrite.

3.Use the newerSO_RCVTIMEO andSO_SNDTIMEO

socket options. The problemwith this approach is that not all implementations support these two socket options.

第一种方法我们可以用ios提供的定时器来实现; 第二种方法在recvfrom方法调用前添加代码如下,这里是设置3秒超时:

[cpp] view plain copy <span style=\ fd_set read_fdset; struct timeval timeout; FD_ZERO(&read_fdset);

FD_SET(socketFileDescriptor, &read_fdset); timeout.tv_sec = 3; timeout.tv_usec = 0; int ret; do { ret = select(socketFileDescriptor + 1, &read_fdset, NULL, NULL, &timeout); } while (ret < 0 && errno == EINTR); if

(ret == 0) { printf(\ return -1; } </span> 第三种方法代码如下:

[cpp] view plain copy <span style=\ struct timeval timeout = {3,0};

setsockopt(socketFileDescriptor, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct

timeval));</span> 设置3秒超时,如果3秒以后无法读到数据,recvfrom方法返回-1,而且errno返回EWOULDBLOCK错误

[cpp] view plain copy <span style=\ n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL); if (n < 0) { if (errno == EWOULDBLOCK) { printf(\ return -1; } else err_sys(\ }</span> 方法二:使用非阻塞式IO

UNP 16.1With a nonblocking socket, if the input operation cannot be satisfied (at leastone byte of data for a TCP socket or a complete datagram for a UDP socket),we see an

immediate return with an error of EWOULDBLOCK. 首先设置非阻塞套接字:

[cpp] view plain copy <span style=\ int ret; int flags = fcntl(socketFileDescriptor,

F_GETFL); if (flags == -1)

{ printf(\ return -1; } flags |= O_NONBLOCK; ret =

fcntl(socketFileDescriptor, F_SETFL, flags); if (ret == -1) { printf(\ return -1; }</span> 然后运行,打印信息如下:>> send success

>> recvfrom fail--errno = 35

>> send success

>> recvfrom fail--errno = 35

>> send success

>> recvfrom fail--errno = 35

>> send success

>> recvfrom fail--errno = 35

>> send success

>> server

said:01&c0:5e:79:fc:0e:0c&192.168.0.1&iShare-R1B011D2199

>> send success

>> recvfrom fail--errno = 35

>> send success

>> recvfrom fail--errno = 35

>> send success

>> recvfrom fail--errno = 35会发现recvfrom方法返回失败的概率很大,而且errno返回EWOULDBLOCK错误,这是因为在非阻塞的情况下有可能因为服务器阻塞导致客户端这边的UDP套接字的接收缓冲区为空, 因此为了解决这个问题我们在recvfrom方法之前添加如下代码:

网络编程Socket之UDP(三)超时设置和非阻塞.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c30xp32lg8f76vac3ljxx41z4g1sgjh0182k_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top