title: Setting winsock select timeout accurately
body: <p>I am trying to make my accept call timeout after a specified time period and I tried following the suggestion here:</p>
  
  <p><a href="https://stackoverflow.com/questions/9683358/winsock-accept-timeout">Winsock accept timeout</a></p>
  
  <p>in which case I pass a TIMEVAL struct to select when I call it, problem is when I set tv.tv_usec to say around 40 minutes or so, the select call times out immediately instead of waiting for the 40 minutes I specified. MSDN states that the timeout for select is the maximum time that it will wait, how do I make it such that select or accept for that matter waits for a specific time period before timing out?</p>
  
  <pre><code>#define WIN32_LEAN_AND_MEAN
  
  #include &lt;Windows.h&gt;
  
  #include &lt;WinSock2.h&gt;
  #include &lt;Ws2tcpip.h&gt;
  
  #include &lt;cstdio&gt;
  #include &lt;tchar.h&gt;
  
  VOID _tmain( int argc, TCHAR *argv[] )
  {
      WSADATA wsaData = { 0 };
      ADDRINFOA hINTs = { 0 };
      PADDRINFOA pResult = NULL;
      SOCKET hServerSocket = INVALID_SOCKET,
             hClientSocket = INVALID_SOCKET;
      TIMEVAL tv = { 0 };
      INT iReturnStatus = -1;
      DWORD dwRecvTimeout = 30000, // Milliseconds
            dwSendTimeout = 30000; // Milliseconds
      fd_set readFDs = { 0 };
  
      if ( WSAStartup( MAKEWORD( 2, 2 ), &amp;wsaData ) )
      {
          _tprintf_s( TEXT( "WSAStartup Failed\n" ) );
          return;
      }
  
      ZeroMemory( &amp;hINTs, sizeof( hINTs ) );
      hINTs.ai_family = AF_INET;
      hINTs.ai_socktype = SOCK_STREAM;
      hINTs.ai_protocol = IPPROTO_TCP;
      hINTs.ai_flags = AI_PASSIVE;
      if ( getaddrinfo( NULL, TEXT( "9001" ), &amp;hINTs, &amp;pResult ) )
      {
          WSACleanup();
          _tprintf_s( TEXT( "getaddrinfo Failed\n" ) );
          return;
      }
  
      if ( ( hServerSocket = socket( pResult -&gt; ai_family, pResult -&gt; ai_socktype, pResult -&gt; ai_protocol ) ) == INVALID_SOCKET )
      {
          freeaddrinfo( pResult );
          WSACleanup();
          _tprintf_s( TEXT( "socket Failed\n" ) );
          return;
      }
  
      int iResult = bind( hServerSocket, ( pResult -&gt; ai_addr ), pResult -&gt; ai_addrlen );
      if ( iResult == SOCKET_ERROR )
      {
          freeaddrinfo( pResult );
          closesocket( hServerSocket );
          WSACleanup();
          _tprintf_s( TEXT( "bind Failed\n" ) );
          return;
      }
      freeaddrinfo( pResult );
  
      if ( listen( hServerSocket, SOMAXCONN ) )
      {
          closesocket( hServerSocket );
          WSACleanup();
          _tprintf_s( TEXT( "listen Failed\n" ) );
          return;
      }
  
      hClientSocket = INVALID_SOCKET;
  
      for ( ;; )
      {
          tv.tv_usec = 2400000000; // microseconds
          FD_ZERO( &amp;readFDs );
          FD_SET( hServerSocket, &amp;readFDs );
          _tprintf( "select()\n" );
          iReturnStatus = select( 0, &amp;readFDs, NULL, NULL, &amp;tv );
  
          // Select Error
          if ( iReturnStatus == SOCKET_ERROR )
          {
              _tprintf( "select Failed\n" );
          }
          // Select Success
          else if ( iReturnStatus )
          {
              // Connection Established On Server Socket
              if ( FD_ISSET( hServerSocket, &amp;readFDs ) )
              {
                  // Accept Client Connection
                  hClientSocket = accept( hServerSocket, NULL, NULL );
                  if ( hClientSocket == INVALID_SOCKET )
                  {
                      _tprintf( "accept Failed\n" );
                  }
                  else
                  {
                      // Set Recv Timeout
                      setsockopt( hClientSocket, SOL_SOCKET, SO_RCVTIMEO, ( const char * ) &amp;dwRecvTimeout, sizeof( dwRecvTimeout ) );
  
                      // Set Send Timeout                 
                      setsockopt( hClientSocket, SOL_SOCKET, SO_SNDTIMEO, ( const char * ) &amp;dwSendTimeout, sizeof( dwSendTimeout ) );
  
                      // Process Client Request(s)
                      // HandleConnection( ClientSocket );
                  }
              }
              // Connection Established On Unknown Socket
              else
              {
                  _tprintf( "Invalid Socket Returned\n" );
              }
          }
          // Select Timeout
          else
          {
              _tprintf( "select Timeout\n" );
          }
  
      }
      if ( hServerSocket != INVALID_SOCKET )
          closesocket( hServerSocket );
  
      return;
  }
  </code></pre>