Skip to content

Latest commit

 

History

History
130 lines (106 loc) · 3.57 KB

Socket-programming-with-UDP.md

File metadata and controls

130 lines (106 loc) · 3.57 KB

Sending and receiving a message over UDP

These methods provide simple string-based communication over a UDP socket connection:

Sending a message

The remoteHost and remotePort parameter values must match the device that is listening for incoming connections and will be receiving the message.

using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Networking;
using Windows.Networking.Sockets;

public static async Task SendStringUdpAsync(HostName remoteHost, 
    string remotePort, string message)
{
    using (var socket = new DatagramSocket())
    {
        var stream = (await socket.GetOutputStreamAsync(
            remoteHost, remotePort)).AsStreamForWrite();
        using (var writer = new StreamWriter(stream))
        {
            await writer.WriteLineAsync(message);
            await writer.FlushAsync();
        }
    }
}

Usage

try
{
    await SendStringUdpAsync(new HostName("192.168.0.5"), 
        "56789", "Hello, world!");
}
catch (Exception)
{
    // Handle appropriate errors.
}

Receiving a message

The specified callback is called everytime a message is received.

The return value is a cleanup action that you should call when you are finished receiving messages.

using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Networking.Sockets;

public static async Task<Action> ReceiveStringUdpAsync(string port,
    Action<string, DatagramSocketMessageReceivedEventArgs> callback)
{
    var socket = new DatagramSocket();
    TypedEventHandler<DatagramSocket, 
        DatagramSocketMessageReceivedEventArgs> handler =
        async (sender, args) =>
        {
            using (var reader = new StreamReader(
                args.GetDataStream().AsStreamForRead()))
            {
                string message = await reader.ReadLineAsync();
                callback(message, args);
            }
        };

    socket.MessageReceived += handler;
    await socket.BindServiceNameAsync(port);

    Action cleanup = () =>
    {
        socket.MessageReceived -= handler;
        socket.Dispose();
    };
    return cleanup;
}

Usage

You can bind to any port that is not already being used by the device. If the port is already in use, the ReceiveStringUdpAsync method will throw an exception.

Action cleanup = null;
try
{
    cleanup = await ReceiveStringUdpAsync("56789", (message, args) =>
    {
        System.Diagnostics.Debug.WriteLine(
            $"\"{message}\" was received from {args.RemoteAddress}.");
    });
}
catch (Exception)
{
    // Handle appropriate errors.
}

// Clean up resources at the appropriate place in your code.  
if (cleanup != null) cleanup();

See also

DatagramSocket class
Exceptions in Windows.Networking.Sockets

Lambda expressions (anonymous methods using the "=>" syntax)
Interpolated strings (strings with a $ prefix)

For code that handles more complex UDP socket communication scenarios, see the P2PHelper project in the QuizGame sample.