我们有一个作为服务运行的.Net应用程序,它在TCP端口上侦听使用TCPClient的流量。它可以成功运行数天和数千个连接,但在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*)提前感谢您:)
发布于 2021-07-01 22:51:44
Simali欢迎来到Stackoverflow
首先,当您发布问题时,要求将代码发布到您的问题中,而不是在评论中。StackOverflows允许您编辑您的问题。此时,我将在我的答案中添加您的代码:
private static void AcceptCallback(IAsyncResult AR) {
Socket socket;
try {
socket = serverSocket.EndAccept(AR);
Log.CreatLog("Client connected, server waiting for request ... ");
socket.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallbackAsync, socket);
serverSocket.BeginAccept(AcceptCallback, null);
}catch (ObjectDisposedException e) {
Log.CreatLog("ObjectDisposedException !!!"); return;
}
}现在,正如堆栈错误所示,错误位于socket.BeginReceive,抛出的异常是SocketException,这是BeginReceive可以抛出的四种不同异常之一:
时出错
可以在此link中检查此信息。
您的程序崩溃是因为您正在处理ObjectDisposedException,而不是像本例中的SocketException那样处理其他程序。因此,您应该添加SocketException并打印程序在发生异常时得到的消息和堆栈错误;如下所示:
private static void AcceptCallback(IAsyncResult AR) {
Socket socket;
try {
socket = serverSocket.EndAccept(AR);
Log.CreatLog("Client connected, server waiting for request ... ");
socket.BeginReceive(buffer, 0, BUFFER_SIZE, SocketFlags.None, ReceiveCallbackAsync, socket);
serverSocket.BeginAccept(AcceptCallback, null);
}catch (ObjectDisposedException e) {
Log.CreatLog("ObjectDisposedException !!!" + e); return;
}
catch (SocketException e) {
Log.CreatLog("SocketException !!!: " + e); return;
}
catch (ArgumentOutOfRangeException e) {
Log.CreatLog("ArgumentOutOfRangeException !!!" + e); return;
}
catch (ArgumentNullException e) {
Log.CreatLog("ArgumentNullException !!!" + e); return;
}
}我添加了其他异常,因为我不知道您的程序是否有其他关于套接字接收的问题。
通过这种方式,您将获得有关错误的更多信息,并且您的程序将有更多机会通过异常。
重要的:以同样的方式在socket.BeginReceive中产生此异常,您可以在此函数中有更多的异常,如BeginAccept。因此,我建议您检查并处理异步套接字抛出的所有可能的异常。
**
升级(7月5日):
**
Simail:为了给我的答案添加更多信息,你的输出不足以给你一个正确的答案。SocketException可能有几个原因,因此您需要打印ErrorCode,正如windows文档所述:
如果收到SocketException,请使用SocketException.ErrorCode属性获取特定的错误代码。获得此代码后,请参阅Windows Sockets version 2 API错误代码文档,了解有关该错误的详细说明。
大多数时候,问题是由代码引起的,但在某些情况下,是由网络触发的异常,但是,使用您共享的信息,不可能知道客户端和服务器之间的问题是什么。
在您共享ErrorCode之后,我可能会帮助您确定问题的原因。
当您发现错误代码时,请让我知道。
https://stackoverflow.com/questions/67971135
复制相似问题