topic | languages | products | ||||||
---|---|---|---|---|---|---|---|---|
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. If you are unfamiliar with Git and GitHub, you can download the entire collection as a 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:
- Use the DatagramSocket class to create a UDP socket for the client to send and receive data.
- Add a handler for a DatagramSocket.MessageReceived event that indicates that a UDP datagram was received on the DatagramSocket object.
- Set the remote endpoint for a UDP network server where packets should be sent using one of the DatagramSocket.ConnectAsync methods.
- Send data to the server using the Streams.DataWriter object which allows a programmer to write common types (integers and strings, for example) on any stream.
- Close the socket.
- Send multicast and broadcast packets using IOutputStreams obtained through DatagramSocket.GetOutputStreamAsync.
The server component of the sample demonstrates the following features:
- Use the DatagramSocket class to create a UDP socket to listen for and receive incoming datagram packets and for sending packets.
- Add a handler for a DatagramSocket.MessageReceived event that indicates that a UDP datagram was received on the DatagramSocket object.
- Bind the socket to a local service name to listen for incoming UDP packets using the DatagramSocket.BindServiceNameAsync method.
- Receive a DatagramSocket.MessageReceived event that indicates that a UDP datagram was received on the DatagramSocket object.
- Receive data from the client using the DatagramSocket.MessageReceived handler. The DatagramSocketMessageReceivedEventArgs object passed to the DatagramSocket.MessageReceived handler allows an app to receive data from the client and also determine the remote address and port that sent the data.
- Close the socket.
- Listen for multicast packets while coexisting with other multicast applications by using the DatagramSocketControl.MulticastOnly control option.
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.
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.
DatagramSocket
DatagramSocketMessageReceivedEventArgs
Windows.Networking
Windows.Networking.Sockets
Windows.Storage.Streams.DataReader
Windows.Storage.Streams.DataWriter
SocketActivityTrigger
StreamSocket sample
SocketActivityTrigger StreamSocket Sample
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
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
Client: Windows 10
Server: Windows Server 2016 Technical Preview
Phone: Windows 10
- If you download the samples ZIP, be sure to unzip the entire archive, not just the folder with the sample you want to build.
- Start Microsoft Visual Studio 2017 and select File > Open > Project/Solution.
- 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.
- Press Ctrl+Shift+B, or select Build > Build Solution.
The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.
- Select Build > Deploy Solution.
- 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.