Compartir a través de


Connecting with sockets (XAML)

Purpose

Send and receive data with TCP or UDP sockets in your Windows Runtime app using features in the Windows.Networking.Sockets namespace.

Sockets provide a low-level interface for sending and receiving network data. The primary classes for use with sockets include the following:

  • DatagramSocket - Used to support network communication using a UDP datagram socket.
  • StreamSocket - Used to support network communication using a TCP stream socket.
  • StreamSocketListener - Used to support listening for an incoming network connection using a TCP stream socket.

The primary classes listed above also have a number of related support classes in the Windows.Networking.Sockets namespace used with these classes.

Developer audience

Designed for use by developers using VB/C#/C++ and XAML interested in enabling the use of sockets for network communications in their Windows Store app. This feature includes TCP and UDP sockets including UDP multicast to enable developers to implement other higher-layer networking protocols. TCP and UDP sockets can be used by an app to make client connections, to listen for connections and operate as a server, or for both client and server operations.

Consider using the following protocols based on your app's requirements.

If your app Use

Connects to a network service that uses an existing protocol (SMTP, POP, IMAP, or MAPI for mail, for example) that is not directly supported by other networking features

TCP or UDP sockets

Connects to another machine on the same local network

TCP or UDP sockets

Requires a simple request\response protocol that can communicate through HTTP proxies.

Representational State Transfer (REST) APIs that are available using C#, VB.NET, and C++ .

Requires socket-like semantics (asynchronous, bi-directional transfers) to connect across the Web, including through HTTP proxies.

WebSockets

 

Sockets are supported by the classes in the Windows.Networking.Sockets and Windows.Networking namespaces.

Windows 8 also introduces a new type of socket, a WebSocket. For more information, see Connecting with WebSockets and the MessageWebSocket and StreamWebSocket classes.

The network isolation feature in Windows 8.1, Windows Phone 8.1, and Windows Server 2012 R2 enables a developer to control and limit network access by a Windows Runtime app. Not all apps may require access to the network. However for those apps that do, Windows 8.1 and Windows Server 2012 R2 provide different levels of access to the network that can be enabled by selecting appropriate capabilities. Network isolation affects any class elements in the Windows.Networking.Sockets and related namespaces that try to access the network. Windows actively enforces network isolation. A call to a class element in the Windows.Networking.Sockets namespace that results in network access may fail because of network isolation if the appropriate network capability has not been enabled.

The network capabilities for an app are configured in the app manifest when the app is built. Network capabilities are usually added using Microsoft Visual Studio 2013 when developing the app. Network capabilities may also be set manually in the app manifest file using a text editor.

For more detailed information on network isolation, see the How to configure network capabilities.

In this section

Topic Description

How to connect with a stream socket

This topic shows how to enable a Windows Runtime app to send and receive data with a TCP stream socket using a StreamSocket.

How to connect with a datagram socket

This topic shows how to enable a Windows Runtime app to send and receive data with a UDP datagram socket using a DatagramSocket. This type of socket can be used to send and receive network packets with low overhead, but reception of the data is not guaranteed.

How to use advanced socket controls

This topic shows how to set advanced socket options on a DatagramSocket, StreamSocket, or StreamSocketListener in a Windows Runtime app.

How to secure socket connections with TLS/SSL

This topic shows how to secure stream socket connections with TLS/SSL when using a StreamSocket in a Windows Runtime app.

How to set timeouts on socket operations

This topic shows how to set timeouts on network socket operations in a Windows Runtime app to limit the time to wait for an operation to complete.

 

Other resources

Adding support for networking

Connecting with WebSockets

How to configure network isolation capabilities

Staying connected in the background

Troubleshoot and debug network connections

Reference

Windows.Networking

Windows.Networking.Connectivity

Windows.Networking.Sockets

Samples

ControlChannelTrigger StreamWebSocket sample

StreamSocket sample