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*)

解决方案 »

  1.   

    删掉(注释掉)你的try...catch,那么vs调试器可以让你立刻调试问题。不要乱写try....catch。否则你就是这样,连调试能力都丧失了,只会看简单的log文本了。
      

  2.   

    当调试器中断在异常处之后,你可以单击“调用堆栈”来看每一层次的调用入口时的变量值。也可以提前设置(条件)断点。也可以在许多处设置变量监视。也可以在代码中使用Debug.Print(....)输出真正的运行日志(而不是仅仅有个异常而已)。进行调试。