将超时与 Alarm() 连接
由于我似乎无法找到原始问题的解决方案,因此我尝试做一些解决方法。我只是想为 TCP 套接字的 connect()
调用设置超时。
- 我希望
connect()
被阻塞,但直到通常的 75 秒超时为止,我想定义自己的超时。 - 我已经尝试过
select()
,它可以解决超时问题,但无法建立连接(这是我最初的问题,如此处)。
所以现在我找到了另一种方法来处理它:只需执行一个阻塞 connect()
调用,但用这样的警报中断它:
signal(SIGALRM, connect_alarm);
int secs = 5;
alarm(secs);
if (connect(m_Socket, (struct sockaddr *)&addr, sizeof(addr)) < 0 )
{
if ( errno == EINTR )
{
debug_printf("Timeout");
m_connectionStatus = STATUS_CLOSED;
return ERR_TIMEOUT;
}
else
{
debug_printf("Other Err");
m_connectionStatus = STATUS_CLOSED;
return ERR_NET_SOCKET;
}
}
with
static void connect_alarm(int signo)
{
debug_printf("SignalHandler");
return;
}
这是我在互联网上的线程 这里在 stackoverflow 上。如果我使用此代码,程序将启动计时器,然后进入 connect()
调用。 5 秒后,信号处理程序被触发(如在控制台上使用 printf()
看到的那样),但此后程序仍保留在 connect()
函数中75秒。实际上,每个描述都说 connect_alarm()
应该中断 connect()
函数,但在我的情况下似乎并非如此。有什么办法可以得到我的问题想要的结果吗?
Since it seems that I can't find a solution to my original problem, I tried to do a little workaround. I'm simply trying to set a timeout to the connect()
call of my TCP Socket.
- I want the
connect()
to be blocking but not until the usual 75 seconds timeout, I want to define my own. - I have already tried
select()
which worked for the timeout but I couldn't get a connection (that was my initial problem as described here ).
So now I found another way to deal with it: just do a blocking connect()
call but interrupt it with an alarm like this :
signal(SIGALRM, connect_alarm);
int secs = 5;
alarm(secs);
if (connect(m_Socket, (struct sockaddr *)&addr, sizeof(addr)) < 0 )
{
if ( errno == EINTR )
{
debug_printf("Timeout");
m_connectionStatus = STATUS_CLOSED;
return ERR_TIMEOUT;
}
else
{
debug_printf("Other Err");
m_connectionStatus = STATUS_CLOSED;
return ERR_NET_SOCKET;
}
}
with
static void connect_alarm(int signo)
{
debug_printf("SignalHandler");
return;
}
This is the solution I found on the Internet in a thread here on stackoverflow. If I use this code the program starts the timer and then goes into the connect()
call. After the 5 seconds the signal handler is fired (as seen on the console with the printf()
), but after that the program still remains within the connect()
function for 75 seconds. Actually every description says that the connect_alarm()
should interrupt the connect()
function but it seems it doesn't in my case. Is there any way to get the desired result for my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
signal
是一个严重未指定的接口,应在新代码中避免使用。在某些版本的 Linux 上,我相信它提供了“BSD 语义”,这意味着(除其他外)默认提供SA_RESTART
。使用
sigaction
代替,不要指定 < code>SA_RESTART,你应该可以开始了。...
嗯,除了普遍的脆弱性和不可避免的竞争条件之外,就是这样。
connect
将为任何信号返回EINTR
,而不仅仅是SIGALARM
。更麻烦的是,如果系统负载很重,调用alarm
和调用connect
之间可能需要5秒以上的时间,这种情况下你将错过信号并永远阻塞connect
。您之前的尝试,使用带有
connect
和select
的非阻塞套接字,是一个更好的主意。我建议调试它。signal
is a massively under-specified interface and should be avoided in new code. On some versions of Linux, I believe it provides "BSD semantics", which means (among other things) that providingSA_RESTART
by default.Use
sigaction
instead, do not specifySA_RESTART
, and you should be good to go....
Well, except for the general fragility and unavoidable race conditions, that is.
connect
will returnEINTR
for any signal, not justSIGALARM
. More troublesome, if the system happens to be under heavy load, it could take more than 5 seconds between the call toalarm
and the call toconnect
, in which case you will miss the signal and block inconnect
forever.Your earlier attempt, using non-blocking sockets with
connect
andselect
, was a much better idea. I would suggest debugging that.虽然设置
警报相对容易(2)
(减少信号处理和系统调用中断的痛苦),超时 TCP 连接尝试的更有效方法是非-blocking connect
,它还允许您启动多个连接并等待所有连接,一次处理一个成功和失败。While it's relatively easy to setup the
alarm(2)
(less the pain of signal handling and system call interruptions), the more efficient way of timing out TCP connection attempts is thenon-blocking connect
, which also allows you to initiate multiple connections and wait on all of them, handling successes and failures one at a time.