Windows OS中无法访问的IP套接字关闭时间 [英] Unreachable IP Socket Close Time in Windows OS
问题描述
这些代码通过用户数据报协议提供发送数据.下面有两个代码.当我将第一个代码用于无法访问的IP地址时,出现了三秒钟的延迟.
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
只需打开新的C#控制台应用程序并粘贴其中的代码即可.(第一码)
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());
}
}
}
}
}
测试1(使用):可访问的IP
测试2(使用):IP无法访问
输出:
Test1 label1 ---> h:mm:ss label2 ---> h:mm:ss(相同时间)
Test2 label1 ---> h:mm:ss label2 ---> h:mm:ss +3秒
(无例外)
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结果:
测试1(使用):可访问的IP->看到了数据.
测试2(使用):IP无法访问->没有数据.
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.
只需打开新的C#控制台应用程序并粘贴其中的代码即可.(第二个代码)
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);
}
}
}
}
测试1(不使用):可达到的Ip
测试2(不使用):IP不可达
Test 1(without using) : Reachable Ip
Test 2(without using) : Unreachable Ip
输出:
Test1 标签1 ---> h:mm:ss(相同时间)label2 ---> h:mm:ss(相同时间)
Test2 label1 ---> h:mm:ss(相同时间)label2 ---> h:mm:ss(相同时间)
(无例外)
WireShark结果:
测试1(不使用):可访问的IP->看到了数据.
测试2(不使用):IP无法访问->没有数据.
WireShark Results:
Test 1(without using) : Reachable Ip --> Data is caught, seen.
Test 2(without using) : Unreachable IP-> No data.
那三秒钟的延迟是什么意思?
我不确定,但是我认为我必须使用"using"块,因为如果不使用这些块,内存使用量将增加很高的水平.两种代码有什么区别?哪一个更可靠?有什么更好的办法吗?我不要三秒钟的延迟.
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.
如何将三秒延迟降低为零?
预先感谢...
新结果
NEW RESULTS
我已经尝试使用Python编程对套接字进行关闭/处理以获取无法访问的IPWindows操作系统中的语言.我得到了相同的结果,即三秒钟的延迟,无法到达IP.但是当我在Ubuntu 15.10中尝试相同的Python代码时,却没有三秒钟的延迟.
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()
推荐答案
您的UdpClient是一次性对象.重新连接之前,应先将其丢弃.
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.
这篇关于Windows OS中无法访问的IP套接字关闭时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!