Socket Async UDP - How to handle client crash

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dav006mex
    New Member
    • May 2010
    • 2

    #1

    Socket Async UDP - How to handle client crash

    I have almost no experience with Network programming on C#.
    I have an Asynchronous UDP socket on a server that sends data to clients.

    If one of the client crashes i get the next Socket Error:

    Message = "An existing connection was forcibly closed by the remote host"
    ErrorCode = 10054
    SocketErrorCode = ConnectionReset

    How do you handle this exception???
    I cant close the socket because there are other clients still receiving messages. I also cant ignore the ICMP message, because i need remove the client from a local list.

    The code:


    Code:
     public void Initialize()
            {
    
                receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    
    
                receiveEndPoint = new IPEndPoint(IPAddress.Any, receivePort);
                receiveSocket.Bind(receiveEndPoint);
                receivePort = (receiveSocket.LocalEndPoint as IPEndPoint).Port;
    
                receiveBuffer = new byte[BufferSize];
                receiveAsyncResult = receiveSocket.BeginReceiveFrom(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, ref receiveEndPoint, new AsyncCallback(NetworkMessageReceivedCallback), receiveSocket);
              ........
              ......
            }
    
    protected void NetworkMessageReceivedCallback(IAsyncResult asyncResult)
            {
                EndPoint remoteEndPoint = null;
                byte[] bytes = null;
    
                try
                {
                    // Finish receiving the message.
                    remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    int bytesRead = receiveSocket.EndReceiveFrom(asyncResult, ref remoteEndPoint);
                    
                    // Copy off to a local buffer. 
                    bytes = new Byte[BufferSize];
                    Buffer.BlockCopy(receiveBuffer, 0, bytes, 0, bytesRead);
    
                   // Continue to hear from port
                    receiveAsyncResult = receiveSocket.BeginReceiveFrom(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None,
                        ref receiveEndPoint, new AsyncCallback(NetworkMessageReceivedCallback), receiveSocket);
                }
                catch (SocketException socketException)
                {
                    // Client is removed from the list
                    if (OnForceDisconnect != null) { OnForceDisconnect(remoteEndPoint); }
                  
                   // Continue to hear from port
                    receiveAsyncResult = receiveSocket.BeginReceiveFrom(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None,
                        ref receiveEndPoint, new AsyncCallback(NetworkMessageReceivedCallback), receiveSocket);
                     
                }
               ..........
               }
    Thanks
    Last edited by tlhintoq; May 19 '10, 10:43 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I'm not sure what your issue is?
    You have the try/catch statement that allows you to catch the error.
    Remove it from the list when you catch the error? You have the socket so you have the endpoints yes?

    Comment

    • dav006mex
      New Member
      • May 2010
      • 2

      #3
      Originally posted by Plater
      I'm not sure what your issue is?
      You have the try/catch statement that allows you to catch the error.
      Remove it from the list when you catch the error? You have the socket so you have the endpoints yes?
      The problem is the next code inside the catch block:

      Code:
      // Continue to hear from port
      
      receiveAsyncResult = receiveSocket.BeginReceiveFrom(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None,ref receiveEndPoint, new AsyncCallback(NetworkMessageReceivedCallback), receiveSocket);
      Even though i catched the exception, I still cant continue to here from the socket. I get the same exception

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I am a bit confused as to how you are getting connection closed errors on a connectionless protocol.

        Comment

        Working...