c++socketstimeoutberkeley-sockets

Disable socket timeout, via setsockopt()


Say I have the following code in C++ that will set the time out for a socket:

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;

How do I disable the timeout using the same command?


Solution

  • You have to set the timeout value to 0. This will do the trick.

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

    A reference can be found here: https://linux.die.net/man/7/socket

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