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

标签 c# python sockets ubuntu udp

这些代码通过用户数据报协议(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/

相关文章:

c# - 如何从中获取日期字符串?

c# - 如何只比较 sql server 2008 中日期时间字段的年份?

c# - 没有路径的文件将在客户端系统中的何处创建

c# - 使用 LINQ 的进程列表?

python - 如何每10秒将数据写入文件

python - Python套接字多处理程序工作池

java - Flex与Java Socket之间的通信

python - 具有多个 cmap 的 matplotlib 热图

python - 在 Windows 上的 Python 中导入 TensorFlow 时出错

sockets - 横向扩展套接字服务器