我有一个windows服务,除其他外,它使用.NET System.Net.Sockets库监听来自远程设备的传入数据。下面的示例中只有一个远程设备每60秒推送数据。
该服务经常会停止,并在服务器的windows日志中显示一条消息(如下所示)。
The process was terminated due to an unhandled exception.
Exception Info: System.Net.Sockets.SocketException Stack:
at System.Net.Sockets.Socket.EndReceive(System.IAsyncResult)
at Uk.Co.RCID.MoteServer.Server.SocketListener.ReceiveCallback(System.IAsyncResult)
at System.Net.LazyAsyncResult.Complete(IntPtr)
at System.Net.ContextAwareResult.CompleteCallback(System.Object)
at System.Threading.ExecutionContext.runTryCode(System.Object)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
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*) 在将此事件的时间与我的上一条日志语句相匹配之后,我发现当远程设备在短时间内两次连接到该服务时(通常,它每60秒连接一次以推送数据),就会发生此致命异常。以下是系统崩溃前的最后一条日志语句。
2011-05-20 13:39:19,545 [6] INFO Uk.Co.RCID.MoteServer.Server.SocketListener [(null)] - Waiting for a connection...
2011-05-20 13:39:19,545 [10] INFO Uk.Co.RCID.MoteServer.Server.SocketListener [(null)] - Connection established on [10.64.128.60:11000]
2011-05-20 13:40:20,982 [6] INFO Uk.Co.RCID.MoteServer.Server.SocketListener [(null)] - Waiting for a connection...
2011-05-20 13:40:20,982 [5] INFO Uk.Co.RCID.MoteServer.Server.SocketListener [(null)] - Connection established on [10.64.128.60:11000]不想发布源代码(如果需要,我可以安排,但需要删除某些细节),套接字侦听代码基于这个示例。
http://msdn.microsoft.com/en-us/library/5w7b7x5f.aspx
任何帮助都是非常感谢的。
大家好,
谢谢你的回应。
我在ReceiveCallback函数中添加了更多的日志记录和异常处理,并在今早的日志文件中找到了以下内容.
2011-05-25 04:52:26,816 [5] INFO Uk.Co.RCID.MoteServer.Server.SocketListener [(null)] - Waiting for a connection...
2011-05-25 04:52:26,816 [10] INFO Uk.Co.RCID.MoteServer.Server.SocketListener [(null)] - Connection established on [10.64.128.60:11000]
2011-05-25 04:53:26,841 [10] WARN Uk.Co.RCID.MoteServer.Server.SocketListener [(null)] - SocketException thrown in ReceiveCallback
2011-05-25 04:53:26,841 [10] WARN Uk.Co.RCID.MoteServer.Server.SocketListener [(null)] - ErrorCode: [10054]
2011-05-25 04:53:26,841 [10] WARN Uk.Co.RCID.MoteServer.Server.SocketListener [(null)] - System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult) at Uk.Co.RCID.MoteServer.Server.SocketListener.ReceiveCallback(IAsyncResult ar)因此,关键是(如上面的一些答案所强调的),捕捉ReceiveCallback函数中的异常,并决定它们是否严重到足以杀死应用程序。
发布于 2011-05-24 10:58:12
当客户端连接无法从半打开状态转换到完全打开状态时,EndAccept可以也确实会引发异常。还请注意,它将在侦听器关闭后抛出一个ObjectDisposedException,因此您必须实际查看异常,并根据出错情况确定该做什么。
当客户端连接强制关闭时,EndReceive通常会抛出,但您必须再次查看异常,并根据抛出的异常确定要做什么。
发布于 2011-05-24 09:34:47
如果您的代码非常接近示例,那么您确实应该有一个尝试捕获:
int read = handler.EndReceive(ar);
在ReceiveCallback函数中,因为它可能像套接字连接在尝试接收时关闭一样简单。
与其扼杀这个过程,你还可以继续下去。
发布于 2011-05-24 10:49:04
在所有回调方法中,您应该始终保持catch (Exception err)。做记录异常。
原因是,在主线程之外的任何其他线程中抛出的所有未处理异常都将终止应用程序。
您应该将套接字回调中的任何异常视为客户端断开连接。当然,您可能会遇到诸如OutOfMemoryException这样的致命异常,这些异常应该会终止应用程序。但在大多数情况下,这是不可能的。
https://stackoverflow.com/questions/6108144
复制相似问题