DatagramSocket.GetOutputStreamAsync(HostName, String) | getOutputStreamAsync(HostName, String) method

Applies to Windows and Windows Phone

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.

Syntax


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

Parameters

remoteHostName

Type: HostName

The remote hostname or remote IP address.

remoteServiceName

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

The remote service name or remote UDP port.

Return value

Type: IAsyncOperation<IOutputStream>

An IOutputStream that representing the asynchronous operation.

Remarks

The GetOutputStreamAsync(HostName, String) method on a DatagramSocket is used to start an operation to get an IOutputStream to a remote network destination specified by a remote hostname and a remote service name. The IOutputStream can then be used to send data to the remote destination.

The GetOutputStreamAsync(HostName, String) method will return an IOutputStream when it completes successfully. Writing to that stream will send datagrams to the remote host and service name specified. The IOutputStream will always send to the remote hostname and remote service name specified in the ConnectAsync method.

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 a UDP port of the remote destination. If the remoteHostName parameter contains a hostname, then the GetOutputStreamAsync(HostName, String) method results in name resolution of the remote hostname. If the remoteServiceName parameter contains a service name, then the GetOutputStreamAsync(HostName, String) method results in resolution of the remote service name to a UDP port.

The remoteHostName and remoteServiceName parameters must be provided. If the remoteHostName parameter is null or the remoteServiceName parameter 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(HostName, String) method is called, the app can set different values for the remoteHostName and remoteServiceName parameters.

The ConnectAsync methods allow an app to send UDP datagrams using a DatagramSocket object to a single network destination.

To receive data from any remote endpoint on the 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 GetOutputStreamAsync method. 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 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. Bind the DatagramSocket to a local service name or UDP port.
  5. The MessageReceived event handler will be invoked whenever a message from the remote endpoint arrives.
  6. Call the GetOutputStreamAsync method to get the OutputStream to send data to the remote endpoint. You can use the information contained in the DatagramSocketMessageReceivedEventArgs from the MessageReceived event handler to establish an OutputStream with the remote endpoint that authored a specific incoming message.

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.

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

ID_CAP_NETWORKING [Windows Phone]

See also

DatagramSocket
HostName
GetOutputStreamAsync(EndpointPair)
IOutputStream

 

 

Show:
© 2014 Microsoft