/// <summary>Set up the socket to use TCP keep alive messages</summary> protected void SetKeepAlive() { try { tcp_keepalive sioKeepAliveVals = new tcp_keepalive(); sioKeepAliveVals.onoff = (uint)1; // 1 to enable 0 to disable sioKeepAliveVals.keepalivetime = this.KeepAliveInactivity; sioKeepAliveVals.keepaliveinterval = this.KeepAliveInterval; IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(sioKeepAliveVals)); Marshal.StructureToPtr(sioKeepAliveVals, p, true); byte[] inBytes = new byte[Marshal.SizeOf(sioKeepAliveVals)]; Marshal.Copy(p, inBytes, 0, inBytes.Length); Marshal.FreeHGlobal(p); byte[] outBytes = BitConverter.GetBytes(0); if (IsConnected(this.socket)) { this.socket.IOControl(IOControlCode.KeepAliveValues, inBytes, outBytes); } else { Close("Currently disconnected. Try to reconnect."); } } catch (Exception ex) { this.OnErrorReceived("Keep Alive", ex); } }
private static void AcceptCallback(IAsyncResult ar) { var TcpClient = ListenSocket.EndAcceptTcpClient(ar); // Keep-Alive特性设置 tcp_keepalive ka = new tcp_keepalive(); ka.onoff = 1; // 启用 ka.keepalivetime = 5 * 60 * 1000; // 无数据收发时每5min发送一次Keep-Alive包 ka.keepaliveinterval = 10000; // Keep-Alive包发送无应答时重发间隔10s byte[] buff = NativeMethods.StructureToByteArray(ka); byte[] ret = new byte[buff.Length]; TcpClient.Client.IOControl(IOControlCode.KeepAliveValues, buff, ret); //添加到新连接列表,定时遍历并关闭不发送数据的连接 AddNewHostAsync(TcpClient); }
public void KeepAlive() { var socket = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); tcp_keepalive tcpKeepalive = new tcp_keepalive(); tcpKeepalive.onoff = 1; tcpKeepalive.keepaliveinterval = 1000; tcpKeepalive.keepalivetime = 1000; int size = Marshal.SizeOf(tcpKeepalive); byte[] arr = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(tcpKeepalive, ptr, true); Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); socket.IOControl(IOControlCode.KeepAliveValues, (byte[])arr, null); }
public static unsafe void EnableKeepaliveWindows(this Socket socket, uint time = 2000, uint interval = 500) { QueryKeepalive(socket); tcp_keepalive keepalive = new tcp_keepalive { onoff = 1, keepalivetime = time, keepaliveinterval = interval }; void *pKeepalive = &keepalive; byte[] bytes = new Span <byte>(pKeepalive, sizeof(tcp_keepalive)).ToArray(); byte[] outVals = new byte[bytes.Length]; int retVal = socket.IOControl(IOControlCode.KeepAliveValues, bytes, outVals); if (retVal != 0) { throw new Exception("IOControl(IOControlCode.KeepAliveValues) != 0"); } QueryKeepalive(socket); }
/// <summary>Set up the socket to use TCP keep alive messages</summary> protected void SetKeepAlive() { try { tcp_keepalive sioKeepAliveVals = new tcp_keepalive(); sioKeepAliveVals.onoff = (uint)1; // 1 to enable 0 to disable sioKeepAliveVals.keepalivetime = this.KeepAliveInactivity; sioKeepAliveVals.keepaliveinterval = this.KeepAliveInterval; IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(sioKeepAliveVals)); Marshal.StructureToPtr(sioKeepAliveVals, p, true); byte[] inBytes = new byte[Marshal.SizeOf(sioKeepAliveVals)]; Marshal.Copy(p, inBytes, 0, inBytes.Length); Marshal.FreeHGlobal(p); byte[] outBytes = BitConverter.GetBytes(0); this._socket.IOControl(IOControlCode.KeepAliveValues, inBytes, outBytes); this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); } catch (Exception ex) { this.OnErrorReceived("Keep Alive", ex); } }
/// <summary>Set up the socket to use TCP keep alive messages</summary> protected void SetKeepAlive() { try { tcp_keepalive sioKeepAliveVals = new tcp_keepalive(); sioKeepAliveVals.onoff = (uint)1; // 1 to enable 0 to disable sioKeepAliveVals.keepalivetime = this.KeepAliveInactivity; sioKeepAliveVals.keepaliveinterval = this.KeepAliveInterval; IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(sioKeepAliveVals)); Marshal.StructureToPtr(sioKeepAliveVals, p, true); byte[] inBytes = new byte[Marshal.SizeOf(sioKeepAliveVals)]; Marshal.Copy(p, inBytes, 0, inBytes.Length); Marshal.FreeHGlobal(p); byte[] outBytes = BitConverter.GetBytes(0); this.socket.IOControl(IOControlCode.KeepAliveValues, inBytes, outBytes); } catch (Exception ex) { this.OnErrorReceived("Keep Alive", ex); } }