Skip to content

Latest commit

 

History

History
 
 

DatagramSocket

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

DatagramSocket sample

This sample shows you 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 was created using one of the universal app templates available in Visual Studio. It shows how its solution is structured so it can run on Windows 10. For more info about how to build apps that target Windows and Windows Phone with Visual Studio, see Build apps that target Windows 10 by using Visual Studio.

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.

Related topics

Reference

DatagramSocket

DatagramSocketMessageReceivedEventArgs

Windows.Networking

Windows.Networking.Sockets

Windows.Storage.Streams.DataReader

Windows.Storage.Streams.DataWriter

SocketActivityTrigger

Samples

StreamSocket sample

SocketActivityTrigger StreamSocket Sample

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

Other - JavaScript and HTML

Adding support for networking (HTML)

Connecting to network services (HTML)

How to send and receive network data with a datagram socket (HTML)

How to set network capabilities (HTML)

How to use advanced socket controls (HTML)

Troubleshooting and debugging network connections

System requirements

Client: Windows 10

Server: Windows Server 2016 Technical Preview

Phone: 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 2015 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 2015 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.