Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5 socket Networking Network , +
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);
            }
        }
 
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?
Posted 10-Oct-11 23:01pm
Edited 14-Oct-11 18:19pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 Smile | :)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 14 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100