c# UdpSocket在发送之后开始接收,在几个包之后停止接收

本文关键字:之后 几个 包之后 UdpSocket 开始 | 更新日期: 2023-09-27 18:06:48

我试图通过UdpSocket从多播地址接收数据。在我通过套接字发送数据之前,套接字没有接收数据。发送后,我可以收到一些包裹,然后我必须再次发送,然后我才能收到更多的包裹。同时从其他主机发送的包丢失了。我认为,这不是像这里这样的防火墙问题:c# UDP套接字直到数据发送后才接收数据,因为白鲨接收所有包。有人能解释一下这种行为吗?

class Program
{
    private static UdpClient _mdnsSocket;
    private static IPEndPoint _mdnsGroup;
    private static IPEndPoint _localEp;
    static void Main(string[] args)
    {
        var interfaces = NetworkInterface.GetAllNetworkInterfaces()
                                         .Where(i => i.OperationalStatus == OperationalStatus.Up)
                                         .ToArray();
        for (int i = 0; i < interfaces.Length; ++i)
        {
            var interf = interfaces[i];
            Console.WriteLine("{0}) Name: {1}", i, interf.Name);
        }
        Console.WriteLine();
        do
        {
            int i;
            Console.Write("Interface: ");
            var line = Console.ReadLine();
            if (int.TryParse(line, out i) && i < interfaces.Length)
            {
                var addr = interfaces[i].GetIPProperties()
                                        .UnicastAddresses.FirstOrDefault(a => a.Address.AddressFamily == AddressFamily.InterNetwork);
                if (addr != null)
                {
                    _localEp = new IPEndPoint(addr.Address, 5353);
                    Console.WriteLine("Choosen IP: {0}", _localEp);
                }
            }
        } while (_localEp == null);

        _mdnsGroup = new IPEndPoint(IPAddress.Parse("224.0.0.251"), 5353);
        _mdnsSocket = new UdpClient();
        _mdnsSocket.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        _mdnsSocket.ExclusiveAddressUse = false;
        _mdnsSocket.Client.Bind(_localEp);
        _mdnsSocket.JoinMulticastGroup(_mdnsGroup.Address, _localEp.Address);
        BeginReceive();
        Console.WriteLine("1 to switch to multicast mode (default)");
        Console.WriteLine("2 to switch to unicast mode");
        Console.WriteLine("s for sending a message");
        Console.WriteLine("ESC for exit");
        ConsoleKey key;
        IPEndPoint ip = _mdnsGroup;
        IPEndPoint unicastip = null;
        var mode = "multicast";
        do
        {
            Console.Write("1/2/s/ESC: ");
            key = Console.ReadKey().Key;
            Console.WriteLine();
            switch (key)
            {
                case ConsoleKey.D1:
                    ip = _mdnsGroup;
                    Console.WriteLine("Switched to multicast mode");
                    mode = "multicast";
                    break;
                case ConsoleKey.D2:
                    Console.Write("Enter new IP (leave empty to use {0}):", unicastip);
                    var input = Console.ReadLine();
                    if (string.IsNullOrEmpty(input))
                    {
                        if (unicastip == null)
                        {
                            Console.WriteLine("error: no last ip!");
                            break;
                        }
                        ip = unicastip;
                        Console.WriteLine("Switched to unicast mode");
                        mode = "unicast";
                    }
                    else
                    {
                        unicastip = new IPEndPoint(IPAddress.Parse(input), 5353);
                        ip = unicastip;
                        Console.WriteLine("Switched to unicast mode");
                        mode = "unicast";
                    }
                    break;
                case ConsoleKey.S:
                    var msg = string.Format("Hello from PC via {0}", mode);
                    var bytes = Encoding.ASCII.GetBytes(msg);
                    Console.WriteLine("Sending to {0}", ip);
                    _mdnsSocket.Send(bytes, bytes.Length, ip);
                    break;
            }
        } while (key != ConsoleKey.Escape);
        _mdnsSocket.Close();
    }
    private static void BeginReceive()
    {
        Console.WriteLine("BeginReceive");
        _mdnsSocket.BeginReceive(ReceiveCallback, _mdnsSocket);
    }
    private static void ReceiveCallback(IAsyncResult ar)
    {            
        try
        {
            var ep = new IPEndPoint(IPAddress.Any, _mdnsGroup.Port);
            var data = _mdnsSocket.EndReceive(ar, ref ep);
            var message = Encoding.ASCII.GetString(data);
            Console.WriteLine(message);
        }
        finally
        {
            BeginReceive();
        }
    }
}

c# UdpSocket在发送之后开始接收,在几个包之后停止接收

毕竟这似乎是一个防火墙问题。当我明确地允许端口5353上的传入UPD时,它正在工作(为什么允许各自程序的所有传入UPD流量不起作用)。我现在用所谓的冲孔机制来解释问题中描述的行为。我说错了,请指正。

同样的问题,但防火墙没有解决它。我用的是Windows 10 PRO。

我发现了另一个不希望的效果:广播消息没有被接收,但是直接的IP消息被接收。

我发现的唯一解决方法是添加一个计时器,每隔10秒在广播中发送一个空字节数组,因此通信保持得足够好。问题是,总是有无用的消息传输到网络,不是那么重,但远非最佳。

关闭套接字并重新启动也解决了这个问题,但它更慢,并且强制GC运行。

我在Unity (Mono)和Visual Studio应用程序上使用相同的UDP类,但这种"效果"只发生在Unity应用程序下。这只发生在IPV4套接字上,IPV6套接字似乎永远不会停止侦听。

我敢肯定这是某种"停止使用IPV4广播"的新操作系统的功能,因为Windows 7工作得很好。

希望能有所帮助。