0

マルチキャスト アドレスから UdpSocket 経由でデータを受信しようとしています。ソケットを介してデータを送信する前に、ソケットはデータを受信しません。送信後、いくつかのパッケージを受信できますが、さらにパッケージを受信するには、再度送信する必要があります。その間、他のホストから送信されたパッケージは失われます。ここのようなファイアウォールの問題ではないと思います.whiresharkはすべてのパッケージを受信するため、データが送信されるまで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();
        }
    }
}
4

2 に答える 2