通过 setsockopt() 禁用套接字超时

Disable socket timeout, via setsockopt()

假设我有以下 C++ 代码,它将设置套接字的超时时间:

struct timeval time_val_struct = { 0 };
time_val_struct.tv_sec = 1;
time_val_struct.tv_usec = 0;
return_value = setsockopt(this->m_fdSocket, SOL_SOCKET, SO_RCVTIMEO,(const char*) &time_val_struct, sizeof(time_val_struct));
    if (return_value == -1)
        return;

如何使用相同的命令禁用超时?

您必须将超时值设置为 0。这样就可以了。

struct timeval time_val_struct;
time_val_struct.tv_sec = 0;
time_val_struct.tv_usec = 0;

可在此处找到参考:https://linux.die.net/man/7/socket

If the timeout is set to zero (the default) then the operation will never timeout