Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings all.

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:

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

                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
Posted
Updated 19-May-10 12:48pm
v2

1 solution

My thingies that i found out with the same probs , these are my hints :

1
I found out that visual studio debug can do a break in debugmode , but that i wouldnt matter so much if this would happen. cause the rest was still connected.

2
I placed the beginrecieve also in a try catch.

3
And i also check if network packets can still be send or recieved.

4
if (ex.SocketErrorCode == SocketError.ConnectionReset)
{
   JTracer.Write(ex, "connection got broken , nothing serious");
}
   else
{
   pTracer.ThrowError(ex, "begin recieve error");
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900