|
@@ -1,4 +1,4 @@
|
|
|
-
|
|
|
+
|
|
|
*
|
|
|
* You can redistribute this program and/or modify it under the terms of
|
|
|
* the GNU Lesser Public License as published by the Free Software Foundation,
|
|
@@ -14,6 +14,25 @@ namespace Utilities
|
|
|
{
|
|
|
public class SocketUtils
|
|
|
{
|
|
|
+ public static void SetKeepAlive(Socket socket, TimeSpan timeout)
|
|
|
+ {
|
|
|
+
|
|
|
+ SetKeepAlive(socket, true, timeout, TimeSpan.FromSeconds(1));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void SetKeepAlive(Socket socket, bool enable, TimeSpan timeout, TimeSpan interval)
|
|
|
+ {
|
|
|
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
|
|
|
+
|
|
|
+ byte[] tcp_keepalive = new byte[12];
|
|
|
+ LittleEndianWriter.WriteUInt32(tcp_keepalive, 0, Convert.ToUInt32(enable));
|
|
|
+ LittleEndianWriter.WriteUInt32(tcp_keepalive, 4, (uint)timeout.TotalMilliseconds);
|
|
|
+ LittleEndianWriter.WriteUInt32(tcp_keepalive, 8, (uint)interval.TotalMilliseconds);
|
|
|
+ socket.IOControl(IOControlCode.KeepAliveValues, tcp_keepalive, null);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
|