DatagramSocket sample

Shows how to a use datagram (UDP) socket to send and receive data using the DatagramSocket and related classes in the Windows.Networking.Sockets namespace in your Universal Windows Platform (UWP) app.

Note: This sample is part of a large collection of UWP feature samples. You can download this sample as a standalone ZIP file from, or you can download the entire collection as a single ZIP file, but be sure to unzip everything to access shared dependencies. For more info on working with the ZIP file, the samples collection, and GitHub, see Get the UWP samples from GitHub. For more samples, see the Samples portal on the Windows Dev Center.

The client component of the sample creates a UDP socket, uses the socket to send and receive data, and closes the socket. The server component of the sample creates a UDP socket to listen for incoming network packets, receives incoming UDP packets from the client, sends data to the client, and closes the socket. This sample is provided in the JavaScript, C#, and C++ programming languages.

The client component of the sample demonstrates the following features:

The server component of the sample demonstrates the following features:

For a sample that shows how to use a stream (TCP) socket to send and receive data in a UWP app, download the StreamSocket sample.

If you are interested in developing an app that is always connected and always reachable using background network notifications in a UWP app, refer to the SocketActivityTrigger documentation. The SocketActivityTrigger StreamSocket Sample is a good starting point, since the programming pattern with DatagramSocket is essentially the same as with StreamSocket.

Network capabilities

This sample requires that network capabilities be set in the Package.appxmanifest file to allow the app to access the network at runtime. These capabilities can be set in the app manifest using Microsoft Visual Studio. For more information on network capabilities, see How to set network capabilities.

Note Network communications using an IP loopback address cannot normally be used for interprocess communication between a Universal Windows Platform (UWP) app and a different process (a different UWP app or a desktop app) because this is restricted by network isolation. Network communication using an IP loopback address is allowed within the same process for communication purposes in a UWP app. For more information, see How to set network capabilities.



Other - C#/VB/C++ and XAML

Adding support for networking (XAML)
Connecting to network services (XAML)
How to send and receive network data with a datagram socket (XAML)
Networking basics
How to use advanced socket controls (XAML)
Troubleshooting and debugging network connections

System requirements

  • Windows 10

Build the sample

  1. If you download the samples ZIP, be sure to unzip the entire archive, not just the folder with the sample you want to build.
  2. Start Microsoft Visual Studio and select File > Open > Project/Solution.
  3. Starting in the folder where you unzipped the samples, go to the Samples subfolder, then the subfolder for this specific sample, then the subfolder for your preferred language (C++, C#, or JavaScript). Double-click the Visual Studio Solution (.sln) file.
  4. Press Ctrl+Shift+B, or select Build > Build Solution.

Run the sample

The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.

Deploying the sample

  • Select Build > Deploy Solution.

Deploying and running the sample

  • To debug the sample and then run it, press F5 or select Debug > Start Debugging. To run the sample without debugging, press Ctrl+F5 or selectDebug > Start Without Debugging.