OGeek|极客世界-中国程序员成长平台

标题: iphone - iOS 中的 BSD 套接字和超时 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:03
标题: iphone - iOS 中的 BSD 套接字和超时

我设置了一个这样的套接字选项:

struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
    perror("setsockopt");
    exit(1);
}

当我这样做时,套接字会在 recv 调用中永远阻塞,但是当我将 tv_usec 设置为 1 时,套接字会按预期超时。这是预期的行为吗?任何有助于理解这一点的帮助将不胜感激。



Best Answer-推荐答案


来自官方POSIX.1 manual page :

The default for this option is the value zero, which indicates that a receive operation will not time out.

因此,如果您自己将超时设置为零,它将与默认设置相同,即无超时。

关于iphone - iOS 中的 BSD 套接字和超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9028466/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4