Home » .Net FrameworkRSS

Existing connection was forcibly closed by a remote host

I have a simulated a simple client server application with a client sending a message to a server (10.48.48.10) on port 67 using a UdpClient. The server has a UdpClient bound to the port 67 to receive the message. The Server receives the message through the UdpClient.EndReceive method processes the message and sends a reply to the client (10.48.48.13) and port 68. Once this is done, I try to rearm the UdpClient through a UdpClient.BeginReceive() and there is an exception "Existing connection was forcibly closed by a remote host".

I have Attached the code I have written to simulate this problem.

********** Server Application ************** running on a machine with IP 10.48.48.10 *************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;

namespace UdpTesterServer
{
    class Program
    {
        private static UdpClient _client;

        private static int ctr = 0;

        private static void Main(string[] args)
        {
            InitializeSocket();
            _client.BeginReceive(OnMessageReceived, null);
            string currentLine = Console.In.ReadLine();
            if (currentLine == "0")
            {
                Process.GetCurrentProcess().Kill();
            }

        }

        private static bool InitializeSocket()
        {
            // Special handling for ReuseAddress since it should be set before the binding occurs, which means
            // the generic constructor must be used, and the binding must be done using the socket base class (.Client)
            // Check if the socket is null (disposed by some other code). If so reinitialize.
            if (_client == null)
            {
                try
                {
                    IPEndPoint ep = new IPEndPoint(IPAddress.Parse("10.48.48.10"), 67);
                    _client = new UdpClient(AddressFamily.InterNetwork);
                    _client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                    _client.Client.Bind(ep);

                }
                catch (Exception ex)
                {

                    _client = null;
                    return false;
                }
            }
            return true;
        }

        private static void OnMessageReceived(IAsyncResult asyncResult)
        {
            MessageReceived(asyncResult);
        }

        private static void MessageReceived(IAsyncResult asyncResult)
        {
            byte[] data = null;
            byte[] tosenddata = new byte[] { 1, 2, 3, 4, 5 };
            IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
            try
            {
                data = _client.EndReceive((IAsyncResult)asyncResult, ref ep);
                if (!CompareByteArrays(data, tosenddata))
                {
                    Trace.WriteLine("unwanted packet processed");
                }
            }
            catch (ObjectDisposedException ex)
            {
                Console.WriteLine("object disposed");
                return;
            }
            catch (SocketException ex)
            {
                Console.WriteLine("Socket ex");
                return;
            }

            catch (Exception ex)
            {
                Console.WriteLine("general ex");
                return;
            }
            IPEndPoint dest = new IPEndPoint(IPAddress.Parse("10.48.48.13"), 68);

            SendDhcpServerResponse(tosenddata, dest);
            _client.BeginReceive(OnMessageReceived, null);
        }

        private static bool SendDhcpServerResponse(byte[] dhcpResponseBytes, IPEndPoint destination)
        {
            try
            {
                _client.Send(dhcpResponseBytes, dhcpResponseBytes.Length, destination);
            }
            catch (Exception ex)
            {
                Console.WriteLine("error sending");
            }
            return true;
        }

        private static bool CompareByteArrays(byte[] array1, byte[] array2)
        {
            if (array1.Length != array2.Length)
                return false;

            for (int i = 0; i < array1.Length; i++)
                if (array1[i] != array2[i])
                    return false;

            return true;
        }
    }
}

**************************************** End Server Code **********************************

 

********************* Client Code ********** running on machine with IP 10.48.48.13 **************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace UdpTesterClient
{
    class Program
    {
        private static UdpClient client;

        static void Main(string[] args)
        {
          
            while (true)
            {
                client = new UdpClient(AddressFamily.InterNetwork);
                client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                Thread.Sleep(5000);
                byte[] data = { 1, 2, 3, 4, 5 };
                IPEndPoint dest = new IPEndPoint(IPAddress.Parse("10.48.48.10"), 67);


                client.Send(data, data.Length, dest);
            }
        }
    }
}

*************************************  End Client Code ***************************************************

Also I switched on WireShark and noticed that there was an ICMP packet that was sent by the Client to the Server with a Destination Unreachable (Port Unreachable) message. I dont understand why the client is sending an ICMP back for a UPDMessage. I suspect that this might be causing the exception that I seen on BeginReceive.

I know that a try catch around the BeginReceive will fix the problem. But the problem should not be there in the first place, using a try catch to bypass this issue is just a work around and not a solution. Experts please help me understand this issue.

 

Thanks,

Confused Engineer.

 

 


<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter