如何使多个连接时设置套接字超时用C? [英] How to set socket timeout in C when making multiple connections?

查看:94
本文介绍了如何使多个连接时设置套接字超时用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个简单的程序,使多个连接到不同的服务器进行状态检查。所有这些连接都构造点播;高达10个连接可以同时创建。我不喜欢一个线程每个插座的想法,所以我做了所有的这些客户端套接字非阻塞,并把它们扔到选择()池。

I'm writing a simple program that makes multiple connections to different servers for status check. All these connections are constructed on-demand; up to 10 connections can be created simultaneously. I don't like the idea of one-thread-per-socket, so I made all these client sockets Non-Blocking, and throw them into a select() pool.

这伟大的工作,直到我的客户抱怨等待的时间太长,才可以得到的错误报告时,目标服务器停止响应。

It worked great, until my client complained that the waiting time is too long before they can get the error report when target servers stopped responding.

我在论坛上检查多个主题。有些人认为,人们可以使用报警()信号或选择()函数调用设置超时。但我处理多个连接,而不是一个。当一个进程宽超时信号发生时,我没有办法区分所有其它连接之间的超时连接。

I've checked several topics in the forum. Some had suggested that one can use alarm() signal or set a timeout in the select() function call. But I'm dealing with multiple connections, instead of one. When a process wide timeout signal happens, I've no way to distinguish the timeout connection among all the other connections.

反正是有改变系统默认的超时时间?

Is there anyway to change the system-default timeout duration ?

推荐答案

您可以使用SO_RCVTIMEO和SO_SNDTIMEO套接字选项来设置超时任何套接字操作,就像这样:

You can use the SO_RCVTIMEO and SO_SNDTIMEO socket options to set timeouts for any socket operations, like so:

    struct timeval timeout;      
    timeout.tv_sec = 10;
    timeout.tv_usec = 0;

    if (setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
                sizeof(timeout)) < 0)
        error("setsockopt failed\n");

    if (setsockopt (sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
                sizeof(timeout)) < 0)
        error("setsockopt failed\n");

编辑:的setsockopt 手册页

SO_SNDTIMEO 是设置输出操作的超时值的选项。它接受与用来限制输出操作完成等待秒和毫秒数timeval结构参数。如果发送操作已经封锁了这么多时间,它与部分计数或错误EWOULDBLOCK返回如果没有数据被送到。在目前的实现,该计时器每个额外的数据传送到协议时重新启动,这意味着该限制适用于输出部分从低水位向高水位标记输出不等。

SO_SNDTIMEO is an option to set a timeout value for output operations. It accepts a struct timeval parameter with the number of seconds and microseconds used to limit waits for output operations to complete. If a send operation has blocked for this much time, it returns with a partial count or with the error EWOULDBLOCK if no data were sent. In the current implementation, this timer is restarted each time additional data are delivered to the protocol, implying that the limit applies to output portions ranging in size from the low-water mark to the high-water mark for output.

SO_RCVTIMEO 是设置输入操作的超时值的选项。它接受与用于限制输入操作完成等待秒和毫秒数timeval结构参数。在当前实现中,这个定时器被每个附加数据由协议接收的时间重新开始,因此限制实际上是不活动定时器。如果接收操作已被封锁这多少时间没有收到额外的数据,它用短计数或如果没有接收到的数据错误EWOULDBLOCK返回。该timeval结构参数必须重新present积极的时间间隔;否则的setsockopt()返回错误EDOM。

SO_RCVTIMEO is an option to set a timeout value for input operations. It accepts a struct timeval parameter with the number of seconds and microseconds used to limit waits for input operations to complete. In the current implementation, this timer is restarted each time additional data are received by the protocol, and thus the limit is in effect an inactivity timer. If a receive operation has been blocked for this much time without receiving additional data, it returns with a short count or with the error EWOULDBLOCK if no data were received. The struct timeval parameter must represent a positive time interval; otherwise, setsockopt() returns with the error EDOM.

这篇关于如何使多个连接时设置套接字超时用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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