//way 1
[DllImport("Kernel32.dll")]
static extern bool QueryPerformanceCounter(ref long count);
[DllImport("Kernel32.dll")]
static extern bool QueryPerformanceFrequency(ref long count);
//(counter2 - counter1) / frequency unit:s
//way 2
DateTime.Now.Ticks();//2 ticks interval unit:100ns
//way 1 is less time than way 2
//tcp default keep alive mechanism:send a package per 2 hour
//change the interval time to 5s or others as a heart mechanism
uint dummy = 0;
byte[] inOptionValues = new byte[Marshal.Sizeof(dummy) * 3];
BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);
BitConverter.GetBytes((uint)2000).CopyTo(inOptionValues, Marshal.Sizeof(dummy));
BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.Sizeof(dummy) * 2);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);