title: Identify network down on tcp socket using TCP_KEEPALIVE in C
body: <p>I am writing a tcp client program that receives data from a tcp server. I am unable to close the connection when the network goes bad (i.e broken network). </p>
  
  <p>I have used SO_KEEPALIVE to allow an application to enable keep-alive packets for a socket connection. The SO_KEEPALIVE property turns on successfuly but i am unable to modify the default value of KeepAliveTime (i.e 2 hrs)</p>
  
  <p>The other avilable option to edit the KeepAliveTime was editing the registry value but i cannot find the registry key at the location <strong>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters</strong> </p>
  
  <p>The following code checks and enables the SO_KEEPALIVE.</p>
  
  <pre><code>char optval;
  char optout;
  int optlen;
  int optlen2;
  optlen = sizeof(optval);
  optlen2 = sizeof(optout);
  got = getsockopt(loFds[i], SOL_SOCKET, SO_KEEPALIVE, &amp;optval, &amp;optlen );
  if(got &lt; 0) 
  {
      printf("getsockopt unsuccessful. Error %ld\n", WSAGetLastError());
  }
  else if (got == 0)  {
  //printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF"));
  printf("SO_KEEPALIVE is %ld\n", optval);
  }
  
  
  optval = 1;
  //optout = 1;
  got = (setsockopt(loFds[i], SOL_SOCKET, SO_KEEPALIVE, &amp;optval, optlen));
   if( got &lt; 0) 
   {
      printf("setsockopt unsuccessful on socket\n");
   }
   else if (got == 0)  {
       printf("SO_KEEPALIVE set on socket\n");
   }
  
  
  
  got = (getsockopt(loFds[i], SOL_SOCKET, SO_KEEPALIVE, &amp;optout, &amp;optlen2 ));
  if( got &lt; 0) 
  {
  
      printf("getsockopt unsuccessful. Error %ld\n", WSAGetLastError());
  
  }
  else if (got == 0)  {
      printf("SO_KEEPALIVE is %ld\n", optval);
  }
  </code></pre>
  
  <p>But i require it to disconnect the socket after a user defined time (may be 5min or so). Kindly suggest any other solution.</p>
  
  <p>Platform: Visual C 2012 on Windows 7 Professional</p>