【发布时间】:2021-08-30 09:53:16
【问题描述】:
我们有一个 .Net 应用程序作为服务运行,使用 TCPClient 在 TCP 端口上侦听流量。它可以成功运行数天和数千个连接,但在某些时候, 服务在 Windows 事件查看器中崩溃并出现此异常:
问题签名:
Application: WindowsService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.Sockets.SocketException
at System.Net.Sockets.Socket.BeginReceive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags, System.AsyncCallback, System.Object)
at WindowsService.Connexion.AcceptCallback(System.IAsyncResult)
at System.Net.LazyAsyncResult.Complete(IntPtr)
at System.Net.ContextAwareResult.CompleteCallback(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Net.ContextAwareResult.Complete(IntPtr)
at System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr)
at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
提前谢谢你:)
【问题讨论】:
-
docs.tibbo.com/taiko/sock_tcp_reconnects 这可能有助于理解它。尝试处理异常并在发生这种情况时使用 Socket.EndReceive。 docs.microsoft.com/en-us/dotnet/api/…
-
嗨,@simali。您能否在处理新连接和接收数据的位置添加代码?
-
嗨,@simali,我修改了答案,但不是最后,所以我需要你分享错误代码,这非常重要,请检查我的升级。
标签: c# .net sockets socketexception