1 out of 3 rated this helpful - Rate this topic

DatagramSocket class

Supports network communication using a UDP datagram socket.

Syntax

Public NotInheritable Class DatagramSocket  
    Inherits Object
    Implements IDisposable

Attributes

ActivatableAttribute(NTDDI_WIN8)
DualApiPartitionAttribute()
MarshalingBehaviorAttribute(Agile)
StaticAttribute(Windows.Networking.Sockets.IDatagramSocketStatics, NTDDI_WIN8)
ThreadingAttribute(Both)
VersionAttribute(NTDDI_WIN8)

Members

The DatagramSocket class has these types of members:

Constructors

The DatagramSocket class has these constructors.

ConstructorDescription
DatagramSocket Creates a new DatagramSocket object.

 

Events

The DatagramSocket class has these events.

EventDescription
MessageReceived An event that indicates that a message was received on the DatagramSocket object.

 

Methods

The DatagramSocket class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
BindEndpointAsync Starts a bind operation on a DatagramSocket to a local hostname and a local service name.
BindServiceNameAsync Starts a bind operation on a DatagramSocket to a local service name.
Close [C++, JavaScript]Closes the DatagramSocket object.
ConnectAsync(EndpointPair) Starts a connect operation on a DatagramSocket to a remote network destination specified as an EndpointPair object.
ConnectAsync(HostName, String) Starts a connect operation on a DatagramSocket to a remote destination specified by a remote hostname and a remote service name.
Dispose [C#, VB]Performs tasks associated with freeing, releasing, or resetting unmanaged resources.
GetEndpointPairsAsync(HostName, String) Gets a list of EndpointPair objects based on a remote hostname and remote service name that can be used to send datagrams to a remote network destination.
GetEndpointPairsAsync(HostName, String, HostNameSortOptions) Gets a list of EndpointPair objects based on a remote hostname and remote service name and the sort order to be used.
GetOutputStreamAsync(EndpointPair) Starts an operation to get an IOutputStream to a remote network destination specified by an EndpointPair object that can then be used to send network data.
GetOutputStreamAsync(HostName, String) Starts an operation to get an IOutputStream to a remote destination specified by a remote hostname and a remote service name that can then be used to send network data.
JoinMulticastGroup Joins a DatagramSocket object to a multicast group.

 

Properties

The DatagramSocket class has these properties.

PropertyAccess typeDescription

Control

Read-onlyGets socket control data on a DatagramSocket object.

Information

Read-onlyGets socket information on the local and remote hostnames and local and remote service names for the DatagramSocket object.

OutputStream

Read-onlyGets the output stream to write to the remote host.

 

Remarks

The DatagramSocket class provides support for network communication using a UDP datagram socket. The DatagramSocket object can be used for both client apps and to listen for incoming UDP data in server apps.

To receive data using a DatagramSocket object, an app must assign the MessageReceived event to an event handler and then call either BindEndpointAsync or BindServiceNameAsync method to bind the DatagramSocket to a local service name or UDP port before calling the ConnectAsync method. If the event handler and bind operation happen after the connection operation, an error will occur.

The typical order of operations is as follows:

  • Create the DatagramSocket.
  • Use the Control property to retrieve a DatagramSocketControl object and set any advanced controls. This step is not normally needed by most apps.
  • Assign the MessageReceived event to an event handler.
  • Bind the DatagramSocket to a local service name or UDP port.
  • Call the ConnectAsync method to bind to remote endpoint. If the app wants to receive data from any remote endpoint on the DatagramSocket object, the ConnectAsync method should not be used since this binds the DatagramSocket to a specific remote endpoint. Instead the BindServiceNameAsync or BindEndpointAsync method should be used.

This class can also be used to join a multicast group and send UDP packets to the multicast group.

Requirements

Minimum supported client

Windows 8 [Windows Store apps, desktop apps]

Minimum supported server

Windows Server 2012 [Windows Store apps, desktop apps]

Minimum supported phone

Windows Phone 8

Namespace

Windows.Networking.Sockets
Windows::Networking::Sockets [C++]

Metadata

Windows.winmd

DLL

Windows.Networking.dll

Capabilities

internetClient
privateNetworkClientServer
ID_CAP_NETWORKING [Windows Phone]

See also

Other resources
Connecting to network services (Windows Store apps using JavaScript and HTML)
Connecting to network services (Windows Store apps using C#/VB/C++ and XAML)
How to use advanced socket controls (Windows Store apps using JavaScript and HTML)
How to use advanced socket controls (Windows Store apps using C#/VB/C++ and XAML)
Quickstart: Connecting to a network resource with a datagram socket (Windows Store apps using JavaScript and HTML)
Troubleshoot and debug network connections
Reference
DatagramSocketControl
DatagramSocketInformation
DatagramSocketMessageReceivedEventArgs
How to use advanced socket controls
IClosable
Object
SetSocketMediaStreamingMode

 

 

Build date: 2/25/2013

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.