These methods provide simple string-based communication over a UDP socket connection:
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();
}
}
}
try
{
await SendStringUdpAsync(new HostName("192.168.0.5"),
"56789", "Hello, world!");
}
catch (Exception)
{
// Handle appropriate errors.
}
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;
}
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();
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.