套接字超时并删除 O_NONBLOCK 选项

发布于 2024-10-18 10:29:06 字数 1805 浏览 3 评论 0原文

我实现了套接字超时并重试,但为了做到这一点,我必须将套接字设置为非阻塞套接字。但是,我需要阻塞套接字。这是我试图解决这两个问题的尝试。这是行不通的。随后的发送调用会阻塞,但从不发送任何数据。当我在没有选择和超时的情况下连接时,后续发送调用正常工作。

参考文献:

代码:

fd_set fdset;
struct timeval tv;
fcntl(dsock, F_SETFL, O_NONBLOCK);
tv.tv_sec = theDeviceTimeout;
tv.tv_usec = 0;
int retries=0;
logi(theLogOutput, LOG_INFO, "connecting to device socket num retrys: %i", theDeviceRetry);
for(retries=0;retries<theDeviceRetry;retries++) {
    connect(dsock, (struct sockaddr *)&daddr, sizeof daddr);


    FD_ZERO(&fdset);
    FD_SET(dsock, &fdset);
    if (select(dsock + 1, NULL, &fdset, NULL, &tv) == 1) {
        int so_error;
        socklen_t slen = sizeof so_error;
        getsockopt(dsock, SOL_SOCKET, SO_ERROR, &so_error, &slen);
        if (so_error == 0) {
            logi(theLogOutput, LOG_INFO, "connected to socket on port %i on %s", theDevicePort, theDeviceIP);
            break;
        } else {
            logi(theLogOutput, LOG_WARN, "connect to %i failed on ip %s because %s retries %i", theDevicePort, theDeviceIP, strerror(errno), retries);
            logi(theLogOutput, LOG_WARN, "failed to connect to device %s", strerror(errno));
            logi(theLogOutput, LOG_WARN, "error: %i %s", so_error, strerror(so_error));
            continue;
        }
    }
}

int opts;
opts = fcntl(dsock,F_GETFL);
logi(theLogOutput, LOG_DEBUG, "clearing nonblock option %i retries %i", opts, retries);
opts ^= O_NONBLOCK;
fcntl(dsock, F_SETFL, opts);

I implemented a socket timeout and retry but in order to do it I had to set the socket as a non-blocking socket. However, I need the socket to block. This was my attempt at a solution to these two problems. This is not working. Subsequent send calls block but never send any data. When I connect without the select and the timeout, subsequent send calls work normally.

References:

Code:

fd_set fdset;
struct timeval tv;
fcntl(dsock, F_SETFL, O_NONBLOCK);
tv.tv_sec = theDeviceTimeout;
tv.tv_usec = 0;
int retries=0;
logi(theLogOutput, LOG_INFO, "connecting to device socket num retrys: %i", theDeviceRetry);
for(retries=0;retries<theDeviceRetry;retries++) {
    connect(dsock, (struct sockaddr *)&daddr, sizeof daddr);


    FD_ZERO(&fdset);
    FD_SET(dsock, &fdset);
    if (select(dsock + 1, NULL, &fdset, NULL, &tv) == 1) {
        int so_error;
        socklen_t slen = sizeof so_error;
        getsockopt(dsock, SOL_SOCKET, SO_ERROR, &so_error, &slen);
        if (so_error == 0) {
            logi(theLogOutput, LOG_INFO, "connected to socket on port %i on %s", theDevicePort, theDeviceIP);
            break;
        } else {
            logi(theLogOutput, LOG_WARN, "connect to %i failed on ip %s because %s retries %i", theDevicePort, theDeviceIP, strerror(errno), retries);
            logi(theLogOutput, LOG_WARN, "failed to connect to device %s", strerror(errno));
            logi(theLogOutput, LOG_WARN, "error: %i %s", so_error, strerror(so_error));
            continue;
        }
    }
}

int opts;
opts = fcntl(dsock,F_GETFL);
logi(theLogOutput, LOG_DEBUG, "clearing nonblock option %i retries %i", opts, retries);
opts ^= O_NONBLOCK;
fcntl(dsock, F_SETFL, opts);

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

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

发布评论

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

评论(2

焚却相思 2024-10-25 10:29:06

获得可写事件并且没有错误后,您需要再次调用 connect(),如文档所述。这会告诉您连接是成功还是失败。

After you get the writeable event and no error you then need to call connect() again, as documented. This tells you whether the connection succeeded or failed.

若相惜即相离 2024-10-25 10:29:06

为什么不使用 SO_RCVTIMEOSO_SNDTIMEO 套接字选项?或者我在你的问题中遗漏了什么?

Why not use SO_RCVTIMEO or SO_SNDTIMEO socket options? Or did I miss something in your question?

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