locked
DatagramSocket Multicast Packet sending problem

    Question

  • Hello,

     I am having trouble with multicast message sending. The packet is not received by any other client. If i use direct IPAddress of other system then packet is send successfully. Here is part of code where i am creating DatagramSocket


    public readonly static string MulticastAddressString = "239.255.255.250";

    int port = 15000; mBClient = new DatagramSocket(); mBClient.MessageReceived += Socket_MessageReceived; mBClient.Control.QualityOfService = SocketQualityOfService.Normal; await mBClient.BindServiceNameAsync(port); HostName multiCastAddress = new HostName(MulticastAddressString); mBClient.JoinMulticastGroup(multiCastAddress);

    Method for sending message.

    async void SendMessage(bool inHost, byte [] inByteMsg, string inAddress, string inPort)
    {
       IOutputStream stream;
       stream = await mBClient.GetOutputStreamAsync(new HostName(MulticastAddressString), inPort);
     
       DataWriter writer = new DataWriter(stream);
       writer.WriteBytes(inByteMsg);
       await writer.StoreAsync();
    }
    

    Thanks

    Friday, June 06, 2014 3:48 PM

Answers

  • Are there any important differences in the code there?  I just did compared them (by looking at the code); it seems like the two do the same things, and in the same order.

    My first thought would be to check your network capabilities (permissions), and in particular make sure you have the Private Networks (client & server) turned on.


    Network Developer Experience Team (Microsoft)

    Monday, June 09, 2014 6:02 PM

All replies

  • Hi,

    For connect to Network please use the code below:

     private async Task<DatagramSocket> StartMulticastListenerAsync(String port)
            {
                DatagramSocket multicastListener = new DatagramSocket();
                // You must register handlers before calling BindXxxAsync  
                multicastListener.MessageReceived += SocketOnMessageReceived;
                await multicastListener.BindServiceNameAsync(port); // Listen on desired port  
                // IPv4 multicast IP addresses: 224.0.0.0 to 239.255.255.255  
                HostName hostname = new HostName("224.168.100.2"); // Pick a multicast IP address  
                multicastListener.JoinMulticastGroup(hostname); // Join socket to the multicast IP address  
                return multicastListener;
    
            }

    And sendMessage and receive message please see the codes below:

     private async Task SendMessage(string message, string port)
            {
                var socket = await StartMulticastListenerAsync(port);
    
                using (var stream = await socket.GetOutputStreamAsync(new HostName("224.168.100.2"), port.ToString()))
                {
                    using (var writer = new DataWriter(stream))
                    {
                        var data = Encoding.UTF8.GetBytes(message);
    
                        writer.WriteBytes(data);
                        await writer.StoreAsync();
                    }
                }
            }
            private async void SocketOnMessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
            {
                var result = args.GetDataStream();
                var resultStream = result.AsStreamForRead(1024);
    
                using (var reader = new StreamReader(resultStream))
                {
                    var text = await reader.ReadToEndAsync();
                    
                }
            }

    Please refer to the link to get more information:

    http://metronuggets.com/2013/03/18/how-to-send-and-receive-a-udp-broadcast-in-windows-phone-8-and-win8/

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, June 09, 2014 2:21 AM
  • Are there any important differences in the code there?  I just did compared them (by looking at the code); it seems like the two do the same things, and in the same order.

    My first thought would be to check your network capabilities (permissions), and in particular make sure you have the Private Networks (client & server) turned on.


    Network Developer Experience Team (Microsoft)

    Monday, June 09, 2014 6:02 PM