基于这里的一些帖子,我编写了以下设置套接字KeepAlive的简单方法,而且它似乎是有效的。在我拔出以太网电缆断开两个服务之间的连接后,我的网络库可以处理在SocketAsyncEventArgs完成事件上传输的0字节,并触发一个断开连接的事件。
但出于某种原因,我需要使用百分之一秒而不是毫秒。我所能找到的所有文档似乎都表明我应该使用毫秒。
(编辑)我现在已经验证了返回的字节是否与其他两个方法匹配:
http://www.codeproject.com/Articles/117557/Set-Keep-Alive-Values
http://social.msdn.microsoft.com/Forums/en-US/35dfb50e-1e14-40c1-af01-f359be4d6a02/how-to-set-socket-keep-alive-timeinterval-values
(编辑)我也有WireSharked这一点,并且可以验证当间隔设置为500时,KeepAlives每5秒就会熄灭一次。
什么给予?在最新版本的.NET中有什么东西坏了吗?
private static byte[] GetKeepAliveOption(bool isEnabled, int keepAliveTime, int keepAliveInterval)
{
//time and interval are in hundredths of a second to work properly
//12 keepalive bytes, 1st 4 for enabled, next 4 for time, next 4 for interval
byte[] bytes = new byte[12];
if (isEnabled)
{
bytes[0] = 1;
}
byte[] keepAliveTimeBytes = BitConverter.GetBytes(KeepAliveTime);
Buffer.BlockCopy(keepAliveTimeBytes, 0, bytes, 4, 4);
byte[] keepAliveIntervalBytes = BitConverter.GetBytes(KeepAliveInterval);
Buffer.BlockCopy(keepAliveIntervalBytes, 0, bytes, 8, 4);
return bytes;
}发布于 2014-02-18 14:46:01
在评论中回答了我自己的问题。对不起,这是我的第一篇帖子,我应该在这样的回答中这样做。
这样做的基础是理解参数:
https://stackoverflow.com/questions/17958232
复制相似问题