简体   繁体   English

为什么System.Net.Sockets.Socket.AcceptAsync在长时间不活动后与ConnectionReset一起完成?

[英]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.ReceiveTimeoutSocket.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.

相关问题 使用 System.Net.Sockets.Socket.AcceptAsync model 时堆栈溢出 - Stack overflow when using the System.Net.Sockets.Socket.AcceptAsync model 为什么刚刚从 AcceptAsync() 返回后 Socket 的 Available > 0? - Why is Socket's Available > 0 just after being returned from AcceptAsync()? System.Net.Sockets.Socket()如何使用其参数? - How does System.Net.Sockets.Socket() use its parameters? “ System.Net.Sockets.Socket不包含'SendTo'的定义” - “System.Net.Sockets.Socket does not contain a definition for 'SendTo'” Socket.AcceptAsync是否需要和“ End”方法调用 - Does Socket.AcceptAsync require and 'End' method call 为什么Socket.AcceptAsync没有触发SocketAsyncEventArgs已完成的事件? - Why Socket.AcceptAsync isn't firing SocketAsyncEventArgs Completed event? 长时间不活动后,.NET应用程序非常慢 - .NET Application very slow after long period of inactivity ArgumentException:值不在预期范围内。System.Net.Sockets.Socket.SetSocketOption - ArgumentException: Value does not fall within the expected range.System.Net.Sockets.Socket.SetSocketOption 为什么我不能在 System.Net.Sockets.Socket 上设置 ReceiveTimeout 属性? - Why can't I set the ReceiveTimeout property on System.Net.Sockets.Socket? 不使用System.Net.Sockets的套接字错误 - Socket Error without using System.Net.Sockets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM