Sockets API Reference

This section provides detailed API reference documentation for networking sockets in Universal Windows Platform (UWP) applications. UWP applications can leverage powerful socket APIs to build network-enabled experiences, from simple client-server communication to complex real-time applications.

Note: This documentation applies to UWP applications targeting Windows 10 and later. Ensure your app's manifest declares the necessary networking capabilities.

Core Socket Classes

The primary classes for socket programming in UWP are found within the Windows.Networking.Sockets namespace.

StreamSocket

Represents a TCP stream socket. It provides a reliable, connection-oriented communication channel.

namespace Windows.Networking.Sockets {
    public sealed class StreamSocket : IClosable {
        // Properties
        public HostName Information { get; }
        public StreamSocketControl Control { get; }
        public StreamSocketListenerInformation ListenerInformation { get; }
        public bool Connected { get; }

        // Methods
        public IAsyncAction ConnectAsync(HostName remoteHostName, string remoteServiceName);
        public IAsyncAction ConnectAsync(HostName remoteHostName, string remoteServiceName, NetworkAdapter networkAdapter);
        public IAsyncAction ConnectAsync(EndpointPair endpointPair);
        public IAsyncAction ConnectAsync(EndpointPair endpointPair, NetworkAdapter networkAdapter);
        public IInputStream InputStream { get; }
        public IOutputStream OutputStream { get; }
        public void Close();
        // ... more methods
    }
}

DatagramSocket

Represents a UDP datagram socket. It provides a connectionless, unreliable datagram communication channel.

namespace Windows.Networking.Sockets {
    public sealed class DatagramSocket : IClosable {
        // Properties
        public DatagramSocketControl Control { get; }

        // Methods
        public IAsyncAction ConnectAsync(HostName remoteHostName, string remoteServiceName);
        public IAsyncAction BindEndpointAsync(string localServiceName, HostName localHostName);
        public IAsyncAction BindServiceNameAsync(string localServiceName);
        public IOutputStream GetOutputStreamAsync(HostName remoteHostName, string remoteServiceName);
        public void Close();
        // ... more methods
    }
}

StreamSocketListener

Used to listen for incoming TCP connections on a specific port.

namespace Windows.Networking.Sockets {
    public sealed class StreamSocketListener : IClosable {
        // Events
        public event TypedEventHandler<StreamSocketListener, StreamSocketListenerConnectionReceivedEventArgs> ConnectionReceived;

        // Methods
        public IAsyncAction BindServiceNameAsync(string❢localServiceName);
        public IAsyncAction BindServiceNameAsync(string❢localServiceName, HostName❢localHostName);
        public void Close();
        // ... more methods
    }
}

Key Concepts

HostName

Represents a network host identified by its name or IP address. It can be an IPv4 address, IPv6 address, or a DNS hostname.

EndpointPair

Represents a pair of endpoints, consisting of a remote hostname/port and a local hostname/port. Useful for specifying specific connection configurations.

StreamSocketControl

Provides access to various control properties for a StreamSocket, such as the keep-alive interval, read/write timeouts, and buffer sizes.

DatagramSocketControl

Provides access to various control properties for a DatagramSocket, such as the broadcast capability and message routing.

IInputStream and IOutputStream

Interfaces for reading from and writing to sockets respectively. These are typically used with StreamSocket.

Common Scenarios

Establishing a TCP Connection (Client)

  1. Create a StreamSocket instance.
  2. Create a HostName object for the server's address.
  3. Call ConnectAsync with the server's hostname and service name (port).
  4. Use the InputStream and OutputStream to send and receive data.
  5. Close the socket when done.

Listening for TCP Connections (Server)

  1. Create a StreamSocketListener instance.
  2. Register an event handler for the ConnectionReceived event.
  3. Call BindServiceNameAsync with the desired local port.
  4. In the ConnectionReceived event handler, you get a StreamSocket for the new connection.
  5. Use the provided socket's InputStream and OutputStream to communicate with the client.
  6. Close the listener and connection sockets when no longer needed.

Sending UDP Datagrams

  1. Create a DatagramSocket instance.
  2. Optionally, call BindEndpointAsync to bind to a local port.
  3. Use GetOutputStreamAsync to get an output stream to a specific remote host and port.
  4. Write data to the output stream.
  5. Close the socket.

Receiving UDP Datagrams

  1. Create a DatagramSocket instance.
  2. Call BindServiceNameAsync to listen on a local port.
  3. Implement the MessageReceived event handler (this would be shown on a more detailed page, as DatagramSocket directly exposes this event).
  4. Inside the handler, you receive the datagram data and the sender's information.
  5. Close the socket.
Tip: For higher-level abstractions like HTTP or WebSocket, consider using dedicated classes like HttpClient or MessageWebSocket, which build upon these socket primitives.

See Also