1

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?

A. Smoliak
  • 438
  • 2
  • 17
  • Either C or C++. Thery are different languages! Said that: what did you try? What about the documentation of the functions is unclear? – too honest for this site Apr 07 '18 at 11:58
  • The same code works when compiling in C++, in fact I am compiling this very code with a C++ compiler for a project. therefore the tag of C++ is applicable – A. Smoliak Apr 07 '18 at 11:58

1 Answers1

1

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

Benjamin J.
  • 1,239
  • 1
  • 15
  • 28
  • Works. Is there a way to set everything to default values? – A. Smoliak Apr 07 '18 at 11:55
  • @A.Smoliak: I don't think that there is a way to set all socket options to the default values. At least I don't know any way to do this. – Benjamin J. Apr 07 '18 at 11:58
  • 1
    There is no magic command to reset the defaults. You should query the socket for its defaults before changing them, then you can restore them later as needed. – Remy Lebeau Apr 09 '18 at 18:40