DatagramSocket.ConnectAsync(HostName, String) | connectAsync(HostName, String) method

Applies to Windows and Windows Phone

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

Syntax


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

Parameters

remoteHostName

Type: HostName

The hostname or IP address of the remote network destination.

remoteServiceName

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.

Remarks

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

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8

Namespace

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

Metadata

Windows.winmd

DLL

Windows.Networking.dll

Capabilities

internetClientServer
privateNetworkClientServer
ID_CAP_NETWORKING [Windows Phone]

See also

DatagramSocket
ConnectAsync(EndpointPair)
HostName
OutputStream
DataWriter
IOutputStream

 

 

Show:
© 2014 Microsoft