title: setting timeout for socket recv TCP
body: <p>I found some hints on how to do it but I dont understand how to use setsockopt. I have an infinite while loop calling recv, I want to timeout and <code>close(cli_socket)</code> if client doesnt send anything in 5 seconds.</p>
  
  <p>If client sends only part of the whole expected message I want to reset timer and give him another 5 seconds.</p>
  
  <p>currentry I have this:</p>
  
  <pre><code>while((cut = buffer.find("\r\n")) == -1)
          {
              struct timeval tv;
              tv.tv_sec = 5;
              setsockopt(cli_socket, SOL_SOCKET, SO_RCVTIMEO,(struct timeval *)&amp;tv,sizeof(struct timeval));
              recv(cli_socket, tmpBuffer, 100, 0);
              buffer += tmpBuffer;
              memset(tmpBuffer, 0, 100);
          }
  </code></pre>