The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DatagramSocket.ConnectAsync(EndpointPair) | connectAsync(EndpointPair) method

Starts a connect operation on a DatagramSocket to a remote network destination specified as an EndpointPair object.


datagramSocket.connectAsync(endpointPair).done( /* Your success and error handlers */ );



Type: EndpointPair

An EndpointPair object that specifies local hostname or IP address, local service name or UDP port, the remote hostname or remote IP address, and the remote service name or remote UDP port for the remote network destination.

Return value

Type: IAsyncAction

An asynchronous connect operation on a DatagramSocket object.


This ConnectAsync(EndPointPair) method on a DatagramSocket is used to define the local and remote endpoint where datagrams will be sent when using the OutputStream property. This method also restricts remote IP addresses of packets that will be accepted to the remote hostname in the endpointPair parameter. Only incoming packets that match the remote endpoint in the endpointPair parameter will trigger the MessageReceived event on the DatagramSocket.

The app can later send network data to the remote network destination by calling the WriteAsync method on the OutputStream property of the DatagramSocket or by passing the OutputStream to a DataWriter object and calling methods of the DataWriter object.

The RemoteHostName property of the EndpointPair passed in the endpointPair parameter can contain either the hostname or IP address of the remote destination. The RemoteServiceName property of the EndpointPair passed in the endpointPair parameter can contain either the service name or a UDP port of the remote destination. If the RemoteHostName property contains a hostname, then the ConnectAsync(EndPointPair) method will resolve the remote hostname to an IP address. If the RemoteServiceName property of the EndpointPair contains a service name, then the ConnectAsync(EndPointPair) method will resolve the remote service name to a UDP port number.

The service name strings that are recognized by default are those service names listed in the %windir%\System32\drivers\etc\services file on the local computer. Any other service name value results in a name service query to domain name system (DNS) servers for DNS SRV records.

The RemoteHostName and RemoteServiceName properties must be provided. If the RemoteHostName is null or if the RemoteServiceName is null or an empty string, an error will occur.

If the EndpointPair object passed in the endpointPair parameter contains a null LocalHostName property, then the system will supply the local IP address that will be used. If the EndpointPair object passed in the endpointPair parameter contains an empty LocalServiceName property, then the system will supply the local UDP port that will be used.

There are two ways of using a DatagramSocket to send UDP datagrams to a remote network destination:

The GetOutputStreamAsync methods allow an app to send UDP datagrams using a DatagramSocket object to multiple network destinations. Each time the GetOutputStreamAsync method is called, the app can set different values for the remoteHostName and remoteServiceName parameters.

To receive data from a single remote endpoint on the DatagramSocket object, an app must assign the MessageReceived event to an event handler and then call the ConnectAsync(EndPointPair) method with the endpointPair parameter set to the remote endpoint information. The MessageReceived event handler must be set before any bind or connect operation, otherwise an error will occur.

The typical order of operations is as follows:

The GetOutputStreamAsync methods also differ from the ConnectAsync method when an app uses the BindEndpointAsync or BindServiceNameAsync method to bind to a local service name or UDP port. With the GetOutputStreamAsync methods, the app will receive packets from any remote destination sent to the local service name or UDP port. With the ConnectAsync methods, the app will only receive packets from the remote destination passed as parameters to the ConnectAsync methods.

The BindServiceNameAsync(String, NetworkAdapter) method can be used to specify a network adapter before calling the ConnectAsync(EndPointPair) method. The specified adapter is used for the bind operation. If after calling BindServiceNameAsync(String, NetworkAdapter) method and the endpointPair parameter passed to ConnectAsync(EndPointPair) specifies a LocalHostName, the ConnectAsync(EndPointPair) method will fail.

The ConnectAsync method only works for unicast IP addresses. When trying to call the ConnectAsync method with a multicast IP address in the remote IP address set in the endpointPair parameter, the asynchronous operation will complete with an error. When passing the error to the SocketError.GetStatus method, the value returned will be SocketErrorStatus.HostNotFound.

Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0


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





Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8


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





See also

ConnectAsync(HostName, String)