Skip to main content
DatagramSocket.ConnectAsync(HostName, String) | connectAsync(HostName, String) method

Starts a connect operation on a DatagramSocket to a remote destination specified by a remote hostname and a remote service name.


datagramSocket.connectAsync(remoteHostName, remoteServiceName).done( /* Your success and error handlers */ );

public IAsyncAction ConnectAsync(
  HostName remoteHostName, 
  string remoteServiceName

Public Function ConnectAsync(
  remoteHostName As HostName,  
  remoteServiceName As String 
) As IAsyncAction

IAsyncAction^ ConnectAsync(
  HostName^ remoteHostName, 
  String^ remoteServiceName



Type: HostName

The hostname or IP address of the remote network destination.


Type: String [JavaScript] | System.String [.NET] | Platform::String [C++]

The service name or UDP port of the remote network destination.

Return value

Type: IAsyncAction

An asynchronous connect operation on a DatagramSocket object.


The ConnectAsync method on a DatagramSocket is used to define the remote hostname or remote IP address and the remote service name or UDP port number where datagrams will be sent when using the OutputStream property. This method also restricts the remote IP addresses of packets that will be accepted to the remoteHostName parameter. Only incoming packets that match the remoteHostName 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 parameter can contain either the hostname or IP address of the remote destination. The remoteServiceName parameter can contain either the service name or an UDP port of the remote destination. If the remoteHostName parameter contains a hostname, then the ConnectAsync method will resolve the remote hostname to an IP address. If the remoteServiceName parameter contains a service name, then the ConnectAsync method will resolve the remote service name to a UDP port number.

The service name strings that are recognized by default by the remoteServiceName parameter 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 parameters must be provided. If the remoteHostName is null or the remoteServiceName is null or an empty string, an error will occur.

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 endpoint on the DatagramSocket object, an app must assign the MessageReceived event to an event handler and then call the ConnectAsync method with the remoteHostName and remoteServiceName parameters 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:

  1. Create the DatagramSocket.
  2. Use the Control property on the DatagramSocket to retrieve a DatagramSocketControl object and set any advanced controls. This step is not normally needed by most apps.
  3. Assign the MessageReceived event to an event handler.
  4. Call the ConnectAsync method to connect to the remote endpoint.
  5. Use the OutputStream property on the DatagramSocket with a DataWriter object to send messages to the remote endpoint.
  6. The MessageReceived event handler will be invoked whenever a message from the remote endpoint arrives.

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 ConnectAsync method only works for unicast IP addresses. When trying to call the ConnectAsync method with a multicast IP address for the remoteHostName 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