C#AsyncCallback套接字使我的应用程序崩溃 [英] C# AsyncCallback Sockets Crash my application

查看:153
本文介绍了C#AsyncCallback套接字使我的应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将套接字与异步回调(服务器和客户端)以及一些一次性(但已同步)资源一起使用.
当我关闭它们时,我会对其进行处置,这通常会导致我的应用程序崩溃而不会引发任何异常.恐怕我在那儿做错了(嗯,嗯...)

我的问题是:

答:在什么情况下C#应用程序会在没有任何通知的情况下崩溃(我的假设是由于处置,或者Async尝试使用处置的对象,但我真的不知道).

B.有没有一种方法可以准确找出应用程序崩溃的位置?好像当我实际调试代码时,一切都按计划进行.

C.能否请您提供一篇体面的文章或解释,说明如何创建良好的插座设计?我当前的设计是接收交替大小的数据包(命令长度(int32)为4个字节,然后为指定长度的数据包).

我的数据接收方法:

I''m using sockets with Async Callbacks (both a server and a client), along with some Disposable (but synchronized) resources.
When I''m turning them off, I''m disposing them, and that''s often cause my app to crash without throwing any exception. I''m afraid that I''m doing something wrong there (Well, duh...)

My questions are:

A. In what cases can a C# application crash without any notice (My assumption is due to disposal, or maybe Async attempts to use disposed objects, but I really have no idea).

B. Is there a way to find out exactly where the application crashes? It seems like when I''m actually debugging the code, everything is going as planned.

C. Could you please offer me a decent article or explanation how to create a good socket design? My current design is receiving alternating sizes of packets (4 bytes for command length (int32), and then packet of that specified length).

My Data Received Method:

private void OnDataReceived(IAsyncResult asyn)
       {
           // Get the packet from the Async Result
           SocketPacket packet = (SocketPacket)asyn.AsyncState;
           try
           {
               // Complete the receive.
               ConnectionSocket client = GetConnection(packet.ClientID);

               // How many bytes were received.
               int length = client.Socket.EndReceive(asyn);

               // Received 0 bytes -> Connection has been disconnected.
               if (length == 0)
               {
                   client.Dispose();
                   m_clients[packet.ClientID - 1] = null;
                   return;
               }

               // Add the extra data that was received.
               packet.AlreadyRead += length;

               // If Message is complete, handle it.
               if (packet.PacketDone)
               {

                   // Was expecting the length of the next command
                   if (client.ExpectToReceive == ExpectToReceive.MessageLength)
                   {
                       // Expect the next packet to be of the specified length.
                       int cmdLength = BitConverter.ToInt32(packet.Data, 0);

                       // Advance - expect next packet to be data.
                       packet.NextMessage(cmdLength);
                       client.ExpectData();
                   }
                   else // Was expecting a command.
                   {

                       // Handle the data

                       // Next packet would be message length, and the length would be 4 bytes.
                       packet.NextMessage(4);
                       client.ExpectLength();

                   }
               }

               // Wait for the next packet.
               WaitForData(packet.ClientID, packet);

           }
           catch (ObjectDisposedException)
           {
               //MessageBox.Show("OnDataReceived: Socket has been closed\n");
           }
           catch (SocketException se)
           {
               if (se.ErrorCode == 10054) // Error code for Connection reset by peer
               {
                   m_clients[packet.ClientID - 1] = null;
               }
               else
                   MessageBox.Show(se.Message);
           }
       }



我的WaitForData方法:



My WaitForData Method:

private void WaitForData(int clientNumber, SocketPacket packet)
{
    try
    {
        // Specify call back function (OnDataReceived) from client.
        if (pfnWorkerCallBack == null)
            pfnWorkerCallBack = new AsyncCallback(OnDataReceived);

        // Get socket.
        Socket s = GetConnection(clientNumber).Socket;

        // Receive the packet, when finished go to call back, send there the packet.
        s.BeginReceive(packet.Data, packet.AlreadyRead, packet.RemainingData,
            SocketFlags.None, pfnWorkerCallBack, packet);
    }
    catch (ObjectDisposedException)
    {
        //MessageBox.Show("Disposed connection");
    }
    catch (SocketException se)
    {
        MessageBox.Show(se.Message);
    }
}



我非常确定问题出在这里...您能指出为什么吗?



I''m pretty sure that the problem is somewhere around here... Can you point out maybe why?

推荐答案

您的应用程序崩溃无一例外-您能告诉我我们发生了什么-它刚刚关闭吗?您是否正在调试器中运行它?您是否尝试过静态分析?

考虑添加全局异常处理程序:

http://msdn.microsoft.com/zh-CN /library/system.appdomain.unhandledexception(v=VS.100).aspx [
Your app crashes without an exception - can you tell us what happens - does it just close? Are you running it in a debugger? Have you tried a run through of static analysis?

Consider adding a global exception handler:

http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception(v=VS.100).aspx[^]

It''s possible you''re missing an exception. If you are not running in debug mode, do so - if you are running the code on a server attempt remote debugging, attach a debugger to the w3wp process or use the visual studio web server in a debugger.

Some more detail of the crash itself would be useful, but thanks for posting the code :)


这篇关于C#AsyncCallback套接字使我的应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆