Share via


Connecting with sockets (HTML)

Send and receive data with TCP or UDP sockets in your Windows Store 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 Javascript/C#/VB.NET/C++ developers interested in enabling the use of sockets for network communications in their Windows Runtime 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.

In This Section

Topic Description
How to connect with a stream socket

Learn how to connect to a network resource with a stream socket using TCP to send and receive data.

How to connecting with a datagram socket

Learn how to connect to a network resource with a datagram socket using UDP to send and receive data.

How to use advanced socket controls

Learn how to use advanced socket controls when using a DatagramSocket, StreamSocket, or StreamSocketListener.

How to secure socket connections with TLS/SSL

Learn how to secure socket connections with TLS/SSL when using a StreamSocket.

How to set timeouts on socket operations

Learn how to set timeouts on network socket operations to limit the time to wait for an operation to complete.

 

Using sockets with network isolation

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 allows a developer to define for each app the scope of required network access. An app without the appropriate scope defined is prevented from accessing the specified type of network, and specific type of network request (outbound client-initiated requests or both inbound unsolicited requests and outbound client-initiated requests). The ability to set and enforce network isolation ensures that if an app does get compromised, it can only access networks where the app has explicitly been granted access. This significantly reduces the scope of the impact on other applications and on Windows.

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 isolation capabilities.

Other resources

Adding support for networking

Connecting with WebSockets

How to configure network isolation capabilities

How to set background connectivity options

Troubleshoot and debug network connections

Reference

Windows.Networking

Windows.Networking.Connectivity

Windows.Networking.Sockets

Samples

DatagramSocket sample

StreamSocket sample