调整套接字连接调用超时

发布于 2024-07-08 11:01:59 字数 103 浏览 3 评论 0原文

在 Win32 环境中是否有任何方法可以“调整”套接字 connect() 调用的超时? 具体来说,我想增加超时长度。

使用的套接字是非阻塞的。

Is there any way in a Win32 environment to "tune" the timeout on a socket connect() call? Specifically, I would like to increase the timeout length.

The sockets in use are non-blocking.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

我恋#小黄人 2024-07-15 11:01:59

是的,这是可能的。

如果您在 之后处于非阻塞模式connect(),通常使用 select() 等待 I/O 准备就绪。 该函数有一个参数用于指定超时值,超时时将返回 0。

Yes, this is possible.

If you're in non-blocking mode after connect(), you normally use select() to wait till I/O is ready. This function has a parameter for specifying the timeout value and will return 0 in case of a timeout.

〃温暖了心ぐ 2024-07-15 11:01:59

您可以尝试使用 SO_RCVTIMEO 和 SO_SNDTIMEO 套接字选项来设置任何套接字操作的超时。 示例:

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");

您还可以尝试alarm()。 样本:

signal( SIGALRM, connect_alarm ); /* connect_alarm is you signal handler */
alarm( secs ); /* secs is your timeout in seconds */
if ( connect( fd, addr, addrlen ) < 0 )
{
    if ( errno == EINTR ) /* timeout, do something below */
        ...
}
alarm( 0 ); /* cancel the alarm */

You can try to use SO_RCVTIMEO and SO_SNDTIMEO socket options to set timeouts for any socket operations. Example:

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");

You can also try alarm(). Sample:

signal( SIGALRM, connect_alarm ); /* connect_alarm is you signal handler */
alarm( secs ); /* secs is your timeout in seconds */
if ( connect( fd, addr, addrlen ) < 0 )
{
    if ( errno == EINTR ) /* timeout, do something below */
        ...
}
alarm( 0 ); /* cancel the alarm */
你的背包 2024-07-15 11:01:59

不,这是不可能的。 默认连接超时可以减少,但不能增加。

No, this is not possible. The default connect timeout can be decreased, but not increased.

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