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 IpOutput:
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()