Skip to content Skip to sidebar Skip to footer

Unreachable Ip Socket Close Time In Windows Os

These codes provide send data via User Datagram Protocol. There are two codes at below. When I use the first code for unreachable Ip address I got the three-second delay. Please

Solution 1:

Your UdpClient is a disposable object. You should dispose it, before you reconnect.

using (var client = new UdpClient()){
                //Please check IP address, It must be unreachable...
                IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.1.41"), 5600);
                client.Connect(ep);
                client.Send(data, data.Length);
            }

or move the connect outside your loop to reuse the same connection.

Solution 2:

The actual difference is that Dispose() method is not called on client in SECOND CODE. But Dispose() is called in FIRST CODE with using (var client = new UdpClient()). The Dispose() method takes 3 seconds additional time when called after attempt to connect unreachable IP Addresses.

You can SECOND CODE as below to notice the delay in printing last label. The delay is caused by Dispose. The client must be declare above try block to use it in finally block.

finally
{
    if (client != null)
        ((IDisposable)client).Dispose();
}
Console.WriteLine(DateTime.Now.ToString("h:mm:ss tt"));

I have also noticed that delay is not caused if unreachable IP Address is out of the domain. For example if IP of my PC is 192.168.1.20 and I try to access 202.22.1.88 then delay is not seen.

Conclusion is: delay is caused by Dispose(). But behavior of Dispose() should be further investigated.

Solution 3:

Decrease three-second delay to zero Solution 1(Using with ThreadPool):


while (true)
{
    Console.WriteLine(DateTime.Now.ToString("h:mm:ss tt"));
    try
    {
        ThreadPool.QueueUserWorkItem(state =>
        {
            using (var client = new UdpClient())
            {                           
                client.Send(data, data.Length, "192.168.1.145", 55600);   
            }                       
        });
        Console.WriteLine(DateTime.Now.ToString("h:mm:ss tt"));
        Console.WriteLine("    ");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    System.Threading.Thread.Sleep(1000);   // to see easily
 }

Decrease three-second delay to zero Solution 1(client=null):


while (true)
{
    Console.WriteLine(DateTime.Now.ToString("h:mm:ss tt"));
    try
    {                 
        var client = new UdpClient();                                                
        client.Send(data, data.Length, "192.168.1.145", 55600);
        client = null;                                            

        Console.WriteLine(DateTime.Now.ToString("h:mm:ss tt"));
        Console.WriteLine("    ");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    System.Threading.Thread.Sleep(1000); // to see easily
}

Post a Comment for "Unreachable Ip Socket Close Time In Windows Os"