UDP 组播 C#

问题描述 投票:0回答:1

我有一个在 Linux (VirtualBox) 中运行的应用程序,它使用 UDP 多播发送消息。我想在 Windows 上运行的 C# 应用程序中接收这些消息。使用 Wireshark,我收到以下信息:“源 = 192.168.6.101”、“目标 = 239.255.255.250”、“端口 = 5697”。我已经在防火墙内允许了该端口。但我仍然无法通过我的 C# 应用程序接收消息

static void Main()
        {
            string multicastAddress = "239.255.255.250"; // Multicast address
            int multicastPort = 5697; // Multicast port

            try
            {
                // Create a UDP client
                UdpClient udpClient = new UdpClient();

                // Allow socket to reuse address
                udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                // Bind the socket to the local port
                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, multicastPort);
                udpClient.Client.Bind(localEndPoint);

                // Join the multicast group
                IPAddress multicastIp = IPAddress.Parse(multicastAddress);
                udpClient.JoinMulticastGroup(multicastIp);

                Console.WriteLine($"Listening for multicast messages on {multicastAddress}:{multicastPort}");

                udpClient.Client.ReceiveTimeout = 10000; // 10-second timeout for receiving messages

                while (true)
                {
                    try
                    {
                        // Receive data
                        IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, multicastPort);
                        byte[] receivedBytes = udpClient.Receive(ref remoteEndPoint);
                        Console.WriteLine($"Received {receivedBytes.Length} bytes from {remoteEndPoint}");
                        Console.WriteLine(BitConverter.ToString(receivedBytes));

                        if (receivedBytes.Length >= 4)
                        {
                            // Interpret the first 4 bytes as a message number (considering endianness)
                            uint receivedMsgNo = BitConverter.ToUInt32(receivedBytes, 0);
                            string messageName = GetMessageName(receivedMsgNo);

                            Console.WriteLine($"Received message number: {receivedMsgNo:X}");
                            Console.WriteLine($"Received message name: {messageName}");
                        }
                        else
                        {
                            Console.WriteLine("Invalid message received.");
                        }
                    }
                    catch (SocketException ex)
                    {
                        Console.WriteLine($"Socket timeout: {ex.Message}");
                        // You can break the loop or handle the timeout accordingly
                    }
                }
            }
            catch (SocketException ex)
            {
                Console.WriteLine($"SocketException: {ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error occurred: {ex.Message}");
            }
        }

我收到的异常如下所示:

Socket timeout: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
c# linux windows udp multicast
1个回答
0
投票

可能听起来很明显,但您是否在发送器上设置了 TTL 套接字选项(默认为 1)?

© www.soinside.com 2019 - 2024. All rights reserved.