gpt4 book ai didi

c# - Windows 操作系统中无法访问的 IP 套接字关闭时间

转载 作者:太空狗 更新时间:2023-10-29 18:33:55 25 4
gpt4 key购买 nike

这些代码通过用户数据报协议(protocol)提供发送数据。下面有两个代码。当我将第一个代码用于无法访问的 IP 地址时,我得到了三秒的延迟。


请看新结果标题


只需打开新的 C# 控制台应用程序并将这些代码粘贴到其中。 (第一个代码)

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.

只需打开新的 C# 控制台应用程序并将这些代码粘贴到其中。 (第二个代码)

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.

三秒延迟是什么意思?
我不确定,但我认为我必须使用“使用” block ,因为如果我不使用 block ,内存使用量会增加到非常高的阶段。两种代码有什么区别?哪个更靠谱?有没有更好的办法?我不想要三秒钟的延迟。

如何将三秒延迟减少到零?

提前致谢...


新结果

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 是一个一次性对象。您应该在重新连接之前处理它。

            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);
}

或将连接移到循环外以重用相同的连接。

关于c# - Windows 操作系统中无法访问的 IP 套接字关闭时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44694061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com