首页 > 解决方案 > C# - 无法接收 UDP 多播帧

问题描述

我使用旧问题(UDP 多播。C#)中的示例来实现 MC 数据包的简单接收器/发送器。

收件人

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;


namespace multiCastRecv
{

    class recv
    {

        recv(string mcastGroup, string port)
        {
            Console.WriteLine("Receive MC from group: {0}, Port: {1}", mcastGroup, port);
            // Create new socket
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            // Create IP endpoint
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, int.Parse(port));

            // Bind endpoint to the socket
            s.Bind(ipep);

            // Multicast IP-address
            IPAddress ip = IPAddress.Parse(mcastGroup);

            // Add socket to the multicast group
            s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Any));

            // Receive messages
            while (true)
            {
                byte[] data = new byte[1024];
                Console.WriteLine("Waiting....");
                int len = s.Receive(data);
                Console.WriteLine("Rcv {0} bytes", len);
                string str = System.Text.Encoding.ASCII.GetString(data, 0, len);
                Console.WriteLine(str.Trim());
            }
        }

        public static void Main(string[] args)
        {
            new recv("224.168.55.25", "4567");
        }
    }
}

发射器:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace multiCastSend
{
    class send
    {
        send(string mcastGroup, string port, string ttl, string rep)
        {
            try
            {
                // Create socket
                Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                // Multicast IP-address
                IPAddress ip = IPAddress.Parse(mcastGroup);

                // Join multicast group
                s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip));

                // TTL
                s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, int.Parse(ttl));

                // Create an endpoint
                IPEndPoint ipep = new IPEndPoint(ip, int.Parse(port));

                // Connect to the endpoint
                s.Connect(ipep);

                // Scan message
                while (true)
                {
                    byte[] b = new byte[10];
                    for (int x = 0; x < b.Length; x++) b[x] = (byte)(x + 65);
                    for (int x = 0; x < int.Parse(rep); x++)
                    {
                        Console.WriteLine("Sending ABCDEFGHIJ...");
                        s.Send(b, b.Length, SocketFlags.None);
                    }
                    Thread.Sleep(1000); // 1000 milliseconds i.e 1sec
                }
            }
            catch (System.Exception e) { Console.Error.WriteLine(e.Message); }
        }
 
        static void Main(string[] args)
        {

            new send("224.168.55.25", "4567", "1", "2");
        }
    }
}

台式机和笔记本电脑都连接到同一个简单的 HUB。通过在接收器平台上运行 Wireshark,我可以正确看到 MC 数据包(见下文),但程序没有接收到:

帧 31355:在线 60 字节(480 位),在接口 0 Ethernet II 上捕获 60 字节(480 位),Src:Flextron_60:22:1f (00:21:cc:60:22:1f),Dst:IPv4mcast_28: 37:19 (01:00:5e:28:37:19) Internet 协议版本 4,Src:10.13.2.165,Dst:224.168.55.25 用户数据报协议,Src 端口:56556,Dst 端口:4567 数据(10 字节)数据:4142434445464748494a【长度:10】

当我在同一台机器上运行接收器和发射器时,它可以工作。

有人知道吗?

标签: c#multicast

解决方案


推荐阅读