简体   繁体   中英

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;
    }

is there anything wrong with the ports?

the recv method is getting blocked but is not receiving the message?

In wireshark I can see the message going from local addr port 10102 to 224.0.0.1 dest_port 0, but the recv is not getting the msg from the multicast addr.

BTW I am running both instances on the same computer. reference : http://msdn.microsoft.com/en-us/library/ekd1t784.aspx

**Got the solution:In send routine

IPEndPoint ep = new IPEndPoint(m_GrpAddr,10102); 

should be

IPEndPoint ep = new IPEndPoint(m_GrpAddr,10101);

the port of receiving**

You need to enable multicast loopback in order to recieve packets sent by yourself.

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

You should use JoinMulticastGroup on the server side as well as the client side. If that fails, you can also try using Wireshark (google it) to see if the packets are actually sent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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