Skip to content

Latest commit

 

History

History
67 lines (43 loc) · 8.58 KB

datagramsocket_connectasync_13692504.md

File metadata and controls

67 lines (43 loc) · 8.58 KB
-api-id -api-type
M:Windows.Networking.Sockets.DatagramSocket.ConnectAsync(Windows.Networking.EndpointPair)
winrt method

Windows.Networking.Sockets.DatagramSocket.ConnectAsync

-description

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

-parameters

-param 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.

-returns

An asynchronous connect operation on a DatagramSocket object.

-remarks

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 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.

Using ConnectAsync with a broadcast IP address such as 255.255.255.255 will connect, but will broadcast only through the first network adapter found. To broadcast on all available adapters, use GetOutputStreamAsync.

-examples

-see-also

ConnectAsync(HostName, String), EndpointPair, OutputStream, DataWriter, IOutputStream

-capabilities

internetClientServer, privateNetworkClientServer, ID_CAP_NETWORKING [Windows Phone]