Metro Nuggets

Bitesized Windows Phone 7, Windows Phone 8 and Windows 8 tidbits

How to Send and Receive a UDP Broadcast in Windows Phone 8 (and Win8)

I’m sure you’ve seen plenty of mobile apps that do a search for a device on the network (say a bluray player) and it magically finds your device on your network. Looks pretty cool, right? Would be great to add that into your app wouldn’t it. Well, as long as you’re not developing for Windows Phone 7, then I will show how.

The Solution

As mentioned, this is only for Windows Phone 8 and Windows 8 as Windows Phone 7 doesn’t support receiving from a UDP broadcast, you can send the message out, but you won’t get any of the responses; you’d need to do a multicast communications for WP7.

So then, the code. This bit of code magically works on both Windows Phone 8 and Windows 8 without any alteration, gotta love that shared networking stack, right!

private async Task SendMessage(string message, int port)
{
    var socket = new DatagramSocket();

    socket.MessageReceived += SocketOnMessageReceived;

    using (var stream = await socket.GetOutputStreamAsync(new HostName("255.255.255.255"), port.ToString()))
    {
        using (var writer = new DataWriter(stream))
        {
            var data = Encoding.UTF8.GetBytes(message);

            writer.WriteBytes(data);
            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();
        Deployment.Current.Dispatcher.BeginInvoke(() =>
                                                        {
                                                            // Do what you need to with the resulting text
                                                            // Doesn't have to be a messagebox
                                                            MessageBox.Show(text);
                                                        });
    }
}

That’s all you need to do. Really, it is.

SL

About these ads

3 Responses to How to Send and Receive a UDP Broadcast in Windows Phone 8 (and Win8)

  1. Pingback: How to Send and Receive a UDP Broadcast in Windows Phone 8 (and Win8)

  2. RagBuster (@RagBuster) April 6, 2013 at 11:25

    Thanks for this snippet of code, Works great on some windows phone 8 code I am trying. How long does the receive process last for? I have sent a SSDP request and seem to receive all the data but is the app always listening?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: