将超时与 Alarm() 连接

发布于 2024-11-29 22:20:25 字数 1440 浏览 1 评论 0原文

由于我似乎无法找到原始问题的解决方案,因此我尝试做一些解决方法。我只是想为 TCP 套接字的 connect() 调用设置超时。

  1. 我希望 connect() 被阻塞,但直到通常的 75 秒超时为止,我想定义自己的超时。
  2. 我已经尝试过 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.

  1. I want the connect() to be blocking but not until the usual 75 seconds timeout, I want to define my own.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵强ㄟ 2024-12-06 22:20:25

signal 是一个严重未指定的接口,应在新代码中避免使用。在某些版本的 Linux 上,我相信它提供了“BSD 语义”,这意味着(除其他外)默认提供 SA_RESTART

使用 sigaction 代替,不要指定 < code>SA_RESTART,你应该可以开始了。

...

嗯,除了普遍的脆弱性和不可避免的竞争条件之外,就是这样。 connect 将为任何信号返回EINTR,而不仅仅是SIGALARM。更麻烦的是,如果系统负载很重,调用alarm和调用connect之间可能需要5秒以上的时间,这种情况下你将错过信号并永远阻塞connect

您之前的尝试,使用带有 connectselect 的非阻塞套接字,是一个更好的主意。我建议调试它。

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 providing SA_RESTART by default.

Use sigaction instead, do not specify SA_RESTART, and you should be good to go.

...

Well, except for the general fragility and unavoidable race conditions, that is. connect will return EINTR for any signal, not just SIGALARM. More troublesome, if the system happens to be under heavy load, it could take more than 5 seconds between the call to alarm and the call to connect, in which case you will miss the signal and block in connect forever.

Your earlier attempt, using non-blocking sockets with connect and select, was a much better idea. I would suggest debugging that.

冷月断魂刀 2024-12-06 22:20:25

虽然设置警报相对容易(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 the non-blocking connect, which also allows you to initiate multiple connections and wait on all of them, handling successes and failures one at a time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文