-api-id | -api-type |
---|---|
M:Windows.Networking.Sockets.DatagramSocket.GetOutputStreamAsync(Windows.Networking.HostName,System.String) |
winrt method |
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.
The remote hostname or remote IP address.
The remote service name or remote UDP port.
An IOutputStream that representing the asynchronous operation.
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:
- Use one of the GetOutputStreamAsync methods.
- After a successful call to one of the ConnectAsync methods, pass the OutputStream property to a DataWriter object and use methods on the DataWriter object to send data.
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:
- Create the DatagramSocket.
- Use the Control property to retrieve a DatagramSocketControl object and set any advanced controls. This step is not normally needed by most apps.
- Assign the MessageReceived event to an event handler.
- Bind the DatagramSocket to a local service name or UDP port.
- The MessageReceived event handler will be invoked whenever a message from the remote endpoint arrives.
- 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.
HostName, GetOutputStreamAsync(EndpointPair), IOutputStream, DatagramSocket sample (Windows 10)
ID_CAP_NETWORKING [Windows Phone]