为什么UDPclient组播不起作用?

时间:2011-10-20 07:53:58

标签: c# udpclient

public void send_multicast(string message)
    {
        UdpClient c = new UdpClient(10102);
        Byte[] sendBytes = Encoding.ASCII.GetBytes(message); 
        IPAddress m_GrpAddr = IPAddress.Parse("224.0.0.1");
        IPEndPoint ep = new IPEndPoint(m_GrpAddr,10102);   
        c.MulticastLoopback=true;
         c.JoinMulticastGroup(m_GrpAddr);
        c.Send(sendBytes,sendBytes.Length,ep);
        Console.WriteLine(message);
    }

    public string recv_multicast()
    {
        Console.WriteLine("was here");
        String strData = "";
        //String Ret = "";
        ASCIIEncoding ASCII = new ASCIIEncoding();
        UdpClient c = new UdpClient(10101);

        // Establish the communication endpoint.
        IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 10101);
         IPAddress m_GrpAddr = IPAddress.Parse("224.0.0.1");

         c.JoinMulticastGroup(m_GrpAddr);
            Byte[] data = c.Receive(ref endpoint);
            strData = ASCII.GetString(data);
            //Ret += strData + "\n";

        return strData;
    }

端口有什么问题吗?

recv方法被阻止但是没有收到消息?

在wireshark中,我可以看到消息从本地地址10102转到224.0.0.1 dest_port 0,但是recv没有从多播地址获取消息。

BTW我在同一台计算机上运行这两个实例。 参考:http://msdn.microsoft.com/en-us/library/ekd1t784.aspx

**得到了解决方案:在发送例程中

IPEndPoint ep = new IPEndPoint(m_GrpAddr,10102); 

应该是

IPEndPoint ep = new IPEndPoint(m_GrpAddr,10101);

接收**的端口

1 个答案:

答案 0 :(得分:1)

您需要启用组播环回才能接收自己发送的数据包。

http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.multicastloopback.aspx

您应该在服务器端和客户端使用JoinMulticastGroup。如果失败,您还可以尝试使用Wireshark(谷歌)查看数据包是否实际发送。

相关问题