[英]Why does System.Net.Sockets.Socket.AcceptAsync complete with ConnectionReset after a long time of inactivity?
It appears that when you call Socket.AcceptAsync
with a valid SocketAsyncEventArgs
and have the SocketAsyncEventArgs.Completed
event rigged properly and no connection is accepted for a very long period of time that it just resets the connection. 看来,当您使用有效的
SocketAsyncEventArgs
调用Socket.AcceptAsync
并正确装配SocketAsyncEventArgs.Completed
事件并且很长一段时间内没有接受任何连接时它只会重置连接。 Though, my Socket.ReceiveTimeout
and Socket.SendTimeout
are both zero. 虽然,我的
Socket.ReceiveTimeout
和Socket.SendTimeout
都是零。
I'm unsure of how to set the timeout for accepting connections and even if it's a good idea at all. 我不确定如何设置接受连接的超时,即使这是一个好主意。 Does anyone have a workaround and perhaps some information as to why this is the default behavior?
有没有人有一个解决方法,也许有一些信息,为什么这是默认行为?
I filed a bug report on Microsoft Connect to see if they have any reasoning behind why the timeout period is unstable. 我在Microsoft Connect上提交了一个错误报告 ,看看他们是否有任何理由说明为什么超时期限不稳定。 Sometimes it times out in five minutes and other times more than two hours.
有时它会在五分钟内超时,其他时间则超过两小时。
After playing around with this for a bit I have found that a single SYN -> SYN-ACK -> RST sequence will raise the SocketAsyncEventArgs.Completed
event and cause the SocketAsyncEventArgs.SocketError
property to become SocketError.ConnectionReset
. 在玩了一下之后,我发现单个SYN - > SYN-ACK - > RST序列将引发
SocketAsyncEventArgs.Completed
事件并导致SocketAsyncEventArgs.SocketError
属性变为SocketError.ConnectionReset
。 It appears this is expected behavior but it's definitely a gotcha and should be documented better. 看来这是预期的行为,但它肯定是一个问题,应该更好地记录。
Anyone port scanning your server and doing a half-open SYN type scan will generate similar traffic and cause the same problem. 任何端口扫描您的服务器并进行半开放SYN类型扫描将产生类似的流量并导致相同的问题。 To prevent denial of service vulnerabilities in the software, one should be handling this special condition.
为了防止软件中的拒绝服务漏洞,应该处理这种特殊情况。
Maybe this could help: Socket problem: TcpClient.GetStream with ReceiveTimeout throws Exception and connected state become false 也许这可能有所帮助: 套接字问题:带有ReceiveTimeout的TcpClient.GetStream抛出异常并且连接状态变为false
If the socket times out, you could also send it keep alive packets every few seconds (minutes). 如果套接字超时,您还可以每隔几秒钟(分钟)发送一次保持活动包。
You could also keep an eye on the connection using Netstat -an | 您还可以使用Netstat -an |来关注连接 findstr /i "PORT#".
findstr / i“PORT#”。 You can find information about that at: TCP Connection States and Netstat Output
您可以在以下位置找到有关该信息的信息: TCP连接状态和Netstat输出
I hope that helps track down your problem. 我希望这有助于追踪您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.