繁体   English   中英

为什么UDPclient组播不起作用?

[英]why is the UDPclient multicast not working?

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没有从多播地址获取消息。

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

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

IPEndPoint ep = new IPEndPoint(m_GrpAddr,10102); 

应该

IPEndPoint ep = new IPEndPoint(m_GrpAddr,10101);

接收港**

需要启用组播环回才能接收自己发送的报文。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM