halo 各位,我们做了一个socket服务,部署后经常崩溃,系统日志记录如下,搞不清楚问题在哪里,请高手们请教下,谢谢。
private static ManualResetEvent AllDone = new ManualResetEvent(false);private static void AcceptCallback(IAsyncResult ar)
{
try
{
AllDone.Set(); Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar); Entity entity = new Entity()
{
SocketInstance = handler
}; // ... handler.BeginReceive(entity.TempBuffer, 0, 1024, 0, new AsyncCallback(ReadCallback), entity);
}
catch (Exception ex)
{
WriteErrorLog(ex);
}
}private static void ReadCallback(IAsyncResult ar)
{
try
{
Entity entity = (Entity)ar.AsyncState;
Socket handler = entity.SocketInstance; int bytReceiveLen = handler.EndReceive(ar); if (bytReceiveLen > 0)
{
entity.LastCommunicaTime = DateTime.Now;
GeneralHelper.ByteHelper.BytArrAppend(ref entity.FullBuffer, entity.TempBuffer, bytReceiveLen); if (bytReceiveLen < 1024)
{
// ...
} handler.BeginReceive(entity.TempBuffer, 0, 1024, 0, new AsyncCallback(ReadCallback), entity);
}
else
{
RemoveEntity(handler);
}
}
catch (SocketException se)
{
WriteErrorLog(ex);
}
catch (Exception ex)
{
WriteErrorLog(ex);
}
}系统日志记录了以下异常:
Application: SocketServer.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.Sockets.SocketException
Stack:
at System.Net.Sockets.Socket.BeginReceive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags, System.AsyncCallback, System.Object)
at SocketServer.Program.ReadCallback(System.IAsyncResult)
at System.Net.LazyAsyncResult.Complete(IntPtr)
at System.Net.ContextAwareResult.CompleteCallback(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*)
private static ManualResetEvent AllDone = new ManualResetEvent(false);private static void AcceptCallback(IAsyncResult ar)
{
try
{
AllDone.Set(); Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar); Entity entity = new Entity()
{
SocketInstance = handler
}; // ... handler.BeginReceive(entity.TempBuffer, 0, 1024, 0, new AsyncCallback(ReadCallback), entity);
}
catch (Exception ex)
{
WriteErrorLog(ex);
}
}private static void ReadCallback(IAsyncResult ar)
{
try
{
Entity entity = (Entity)ar.AsyncState;
Socket handler = entity.SocketInstance; int bytReceiveLen = handler.EndReceive(ar); if (bytReceiveLen > 0)
{
entity.LastCommunicaTime = DateTime.Now;
GeneralHelper.ByteHelper.BytArrAppend(ref entity.FullBuffer, entity.TempBuffer, bytReceiveLen); if (bytReceiveLen < 1024)
{
// ...
} handler.BeginReceive(entity.TempBuffer, 0, 1024, 0, new AsyncCallback(ReadCallback), entity);
}
else
{
RemoveEntity(handler);
}
}
catch (SocketException se)
{
WriteErrorLog(ex);
}
catch (Exception ex)
{
WriteErrorLog(ex);
}
}系统日志记录了以下异常:
Application: SocketServer.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.Sockets.SocketException
Stack:
at System.Net.Sockets.Socket.BeginReceive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags, System.AsyncCallback, System.Object)
at SocketServer.Program.ReadCallback(System.IAsyncResult)
at System.Net.LazyAsyncResult.Complete(IntPtr)
at System.Net.ContextAwareResult.CompleteCallback(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*)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货