我有一个Windows服务,其中包括使用.NET System.Net.Sockets库监听来自远程设备的传入数据。下面的示例只有一个远程设备每60秒推送一次数据。TCP套接字连接
每隔一段时间,该服务都会停止,并在服务器的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
函数中的例外情况,并确定它们是否足够严重以杀死您的应用程序。
在捕获异常的'InnerException'属性中是否存在内部异常?我认为你的代码中可能会有东西被抛出,你从套接字接收数据。 – Nick 2011-05-24 09:21:09
不幸的是我没有这个信息。由于很少发生错误,因此需要一些时间才能通过更多日志记录重新创建问题。我会尝试用模拟远程设备重新创建它。 – user767428 2011-05-24 09:28:20