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.
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)
- Create a
StreamSocketinstance. - Create a
HostNameobject for the server's address. - Call
ConnectAsyncwith the server's hostname and service name (port). - Use the
InputStreamandOutputStreamto send and receive data. - Close the socket when done.
Listening for TCP Connections (Server)
- Create a
StreamSocketListenerinstance. - Register an event handler for the
ConnectionReceivedevent. - Call
BindServiceNameAsyncwith the desired local port. - In the
ConnectionReceivedevent handler, you get aStreamSocketfor the new connection. - Use the provided socket's
InputStreamandOutputStreamto communicate with the client. - Close the listener and connection sockets when no longer needed.
Sending UDP Datagrams
- Create a
DatagramSocketinstance. - Optionally, call
BindEndpointAsyncto bind to a local port. - Use
GetOutputStreamAsyncto get an output stream to a specific remote host and port. - Write data to the output stream.
- Close the socket.
Receiving UDP Datagrams
- Create a
DatagramSocketinstance. - Call
BindServiceNameAsyncto listen on a local port. - Implement the
MessageReceivedevent handler (this would be shown on a more detailed page, asDatagramSocketdirectly exposes this event). - Inside the handler, you receive the datagram data and the sender's information.
- Close the socket.
HttpClient or MessageWebSocket, which build upon these socket primitives.