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

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

问题描述

我正在编写一个简单的程序,它与不同的服务器建立多个连接以进行状态检查.所有这些连接都是按需构建的;最多可以同时创建 10 个连接.我不喜欢每个套接字一个线程的想法,因此我将所有这些客户端套接字设为非阻塞,并将它们放入 select() 池中.

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.

我查看了论坛中的几个主题.有人建议可以使用 alarm() 信号或在 select() 函数调用中设置超时.但我正在处理多个连接,而不是一个.当进程范围的超时信号发生时,我无法在所有其他连接中区分超时连接.

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, &timeout,
                sizeof timeout) < 0)
        error("setsockopt failed
");

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

来自 setsockopt 手册页:

SO_SNDTIMEO 是为输出操作设置超时值的选项.它接受一个 struct 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 是为输入操作设置超时值的选项.它接受带有用于限制等待输入操作完成的秒数和微秒数的 struct timeval 参数.在当前的实现中,每当协议接收到额外的数据时,这个定时器就会重新启动,因此限制实际上是一个不活动的定时器.如果一个接收操作被阻塞了这么长时间而没有接收到额外的数据,它会返回一个短计数或者如果没有接收到数据则返回错误 EWOULDBLOCK.struct timeval 参数必须表示正时间间隔;否则,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天全站免登陆