C# KeepAlive的相关设置
网上有很多相关KeepAlive的内容,终于找到了有关C#的这方面资料,设置了下,有行可靠!
TcpListener myListener = new TcpListener(IPAddress.Any, port);//绑定端口IP信息 myListener.Start();//开始监听 TcpClient newClient=myListener.AcceptTcpClient();//接受请求 newClient.Client.IOControl(IOControlCode.KeepAliveValues, KeepAlive(1, 30000, 10000), null);//设置Keep-Alive参数 private byte[] KeepAlive(int onOff, int keepAliveTime, int keepAliveInterval) { byte[] buffer = new byte[12]; BitConverter.GetBytes(onOff).CopyTo(buffer, 0); BitConverter.GetBytes(keepAliveTime).CopyTo(buffer, 4); BitConverter.GetBytes(keepAliveInterval).CopyTo(buffer, 8); return buffer; }
KeepAlive函数参数说明:
onOff:是否开启KeepAlive
keepAliveTime:开始首次KeepAlive探测前的TCP空闭时间
keepAliveInterval: 两次KeepAlive探测间的时间间隔
Keep-Alive的位置我是放在收到客户端以后,对每个连入的客户端都设置一次。
相关文章:http://www.cnblogs.com/lidabo/p/4253356.html
时间: 2024-10-07 08:21:02