Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Look New Results Title


JUST OPEN NEW C# CONSOLE APP AND PASTE THESE CODES IN IT. (FIRST CODE)

using System;
using System.Net;
using System.Net.Sockets;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] data = { 1, 20, 60, 44, 244 };
            while (true)
            {
                Console.WriteLine(DateTime.Now.ToString("h:mm:ss tt"));
                try
                {
                    using (var client = new UdpClient())
                    {
                        // Please check IP Address, It must be unreachable...
                       // IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.1.141"), 55600);
                      //  client.Connect(ep);
                        client.Send(data, data.Length, "192.168.1.141" , 55600);
                    }
                    Console.WriteLine(DateTime.Now.ToString("h:mm:ss tt"));
                    Console.WriteLine("    ");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
    }
}

Test 1(with using): Reachable IP
Test 2(with using): Unreachable IP
Output:
Test1 label1 ---> h:mm:ss label2 ---> h:mm:ss (Same Time)
Test2 label1 ---> h:mm:ss label2 ---> h:mm:ss +3 second
(No exception)

WireShark Results:
Test 1(with using) : Reachable Ip --> Data is caught, seen.
Test 2(with using) : Unreachable IP-> No data.

When I use without "using" blocks, I didn't get the three-second delay.

JUST OPEN NEW C# CONSOLE APP AND PASTE THESE CODES IN IT. (SECOND CODE)

using System;
using System.Net;
using System.Net.Sockets;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] data = { 1, 20, 60, 44, 244 };
            while (true)
            {
                Console.WriteLine(DateTime.Now.ToString("h:mm:ss tt"));
                try
                {
                    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, "192.168.1.141", 55600);

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

Test 1(without using) : Reachable Ip
Test 2(without using) : Unreachable Ip

Output:
Test1 label1 ---> h:mm:ss (Same Time) label2 ---> h:mm:ss (Same Time)
Test2 label1 ---> h:mm:ss (Same Time) label2 ---> h:mm:ss (Same Time)
(No exception)

WireShark Results:
Test 1(without using) : Reachable Ip --> Data is caught, seen.
Test 2(without using) : Unreachable IP-> No data.

What is the mean of that three-second delay?
I am not sure but I think I have to use "using" blocks because if I didn't use the blocks memory usage will increase very high stage. What is the difference between both codes? Which one is more reliable? Is there any better way? I don't want the three-second delay.

How to decrease three-second delay to zero?

Thanks in advance...


NEW RESULTS

I have tried socket Close/Dispose for unreachable IP with Python Programming Language in Windows OS. I got same result namely three-second delay for unreachable IP. But when I try same Python code within Ubuntu 15.10, I didn't get the three-second delay.

import socket
import datetime

IPADDR = '192.168.1.141'
PORTNUM = 5600
PACKETDATA = "f1a525da11f6".encode()

while(True):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
    s.connect((IPADDR, PORTNUM))
    s.send(PACKETDATA)
    print(datetime.datetime.now())
    s.close()
like image 717
Salih Karagoz Avatar asked Jun 22 '17 08:06

Salih Karagoz


2 Answers

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.

like image 173
Peter Avatar answered Sep 28 '22 23:09

Peter


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.

like image 35
MKR Avatar answered Sep 28 '22 22:09

MKR