using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Sockets; using System.Runtime.InteropServices; namespace CSharpUtil { /// /// 辅助类 /// [Obsolete("暂时不太能跨平台",true)] [CLSCompliant(false)] public static class SocketUtil { public static void SetKeepAlive(this Socket sock, int interval_ms) { uint dummy = 0; byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3]; BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0); BitConverter.GetBytes((uint)interval_ms).CopyTo(inOptionValues, Marshal.SizeOf(dummy)); BitConverter.GetBytes((uint)interval_ms).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2); sock.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null); } } }