This sample shows you how to a use stream (TCP) socket to send and receive data using the StreamSocket and related classes in the Windows.Networking.Sockets namespace in your Windows Runtime app.
Note This sample was created using one of the universal app templates available in Visual Studio. It shows how its solution is structured so it can run on both Windows 8.1 and Windows Phone 8.1. For more info about how to build apps that target Windows and Windows Phone with Visual Studio, see Build apps that target Windows and Windows Phone 8.1 by using Visual Studio.
The client component of the sample creates a TCP socket to make a network connection, uses the socket to send data, and closes the socket. The server component sets up a TCP listener that provides a connected socket for every incoming network connection, uses the socket to receive data from the client, and closes the socket. This sample is provided in the JavaScript, C#, VB, and C++ programming languages.
The client component of the sample demonstrates the following features:
- Use the StreamSocket class to create a TCP socket.
- Make a network connection to a TCP network server using one of the StreamSocket.ConnectAsync methods.
- Send data to the server using the Streams.DataWriter object which allows a programmer to write common types (integers and strings, for example) on any stream.
- Close the socket.
- Attempt a socket connection using SSL to web server at port 443 (HTTPS), evaluate the server certificate validity, and display its properties. A certificate error is expected since the self-signed certificate is not trusted and issued to a different site name.
The server component of the sample demonstrates the following features:
- Use the StreamSocketListener class to create a TCP socket to listen for an incoming TCP connection.
- Bind the socket to a local service name to listen for an incoming network connection using the StreamSocketListener.BindServiceNameAsync method.
- Receive a StreamSocketListener.ConnectionReceived event that indicates that a connection was received on the StreamSocketListener object.
- Receive data from the client using the Streams.DataReader object which allows a programmer to read common types (integers and strings, for example) on any stream.
- Close the socket.
Note Use of this sample requires network access using the loopback interface.
For a sample that shows how to use a datagram (UDP) socket to send and receive data in a Windows Runtime app, download the DatagramSocket sample.
For a sample that shows how to use a StreamSocket so that the app is always connected and always reachable using background network notifications in a Windows Store app, download the ControlChannelTrigger TCP socket sample.
Network capabilities
This sample requires that network capabilities be set in the Package.appxmanifest file to allow the app to access the network at runtime. These capabilities can be set in the app manifest using Microsoft Visual Studio.
To build the Windows version of the sample, set the following network capabilities:
-
Private Networks (Client & Server): The sample has inbound and outbound network access on a home or work network (a local intranet). Even though this sample by default runs on loopback, it needs either the Private Networks (Client & Server) or Internet (Client & Server) capability in order to accept connections using the StreamSocketListener object. The Private Networks (Client & Server) capability is represented by the Capability name = "privateNetworkClientServer" tag in the app manifest. The Internet (Client & Server) capability is represented by the Capability name = "internetClientServer" tag in the app manifest.
-
If the sample is modified to connect to the server component running on a different device on the Internet (a more typical app), the Internet (Client) capability must be set for the client component. This is represented by the Capability name = "internetClient". The Internet (Client & Server) capability must be set for the server component. This is represented by the Capability name = "internetClientServer" tag in the app manifest.
To build the Windows Phone version of the sample, set the following network capabilities:
-
Internet (Client & Server): This sample has complete access to the network for both client operations (outbound-initiated access) and server operations (inbound-initiated access). This allows the app to accept connections using the StreamSocketListener object from the Internet or from a local intranet. This is represented by the Capability name = "internetClientServer" tag in the app manifest.
Note On Windows Phone, there is only one network capability which enables all network access for the app.
For more information on network capabilities, see How to set network capabilities.
Note Network communications using an IP loopback address cannot normally be used for interprocess communication between a Windows Runtime app and a different process (a different Windows Runtime app or a desktop app) because this is restricted by network isolation. Network communication using an IP loopback address is allowed within the same process for communication purposes in a Windows Runtime app. For more information, see How to set network capabilities.
To obtain an evaluation copy of Windows 8.1, go to Windows 8.1.
To obtain an evaluation copy of Microsoft Visual Studio 2013 Update 2, go to Microsoft Visual Studio 2013.
Note For Windows 8 app samples, download the Windows 8 app samples pack. The samples in the Windows 8 app samples pack will build and run only on Microsoft Visual Studio 2012.
Related topics
- Other - C#/VB/C++ and XAML
- Adding support for networking (XAML)
- Connecting to network services (XAML)
- How to secure socket connections with TLS/SSL (XAML)
- How to send and receive network data with a stream socket (XAML)
- How to set network capabilities (XAML)
- How to use advanced socket controls (XAML)
- Proximity and tapping (XAML)
- Staying connected in the background (XAML)
- Troubleshooting and debugging network connections
- Other - JavaScript and HTML
- Adding support for networking (HTML)
- Connecting to network services (HTML)
- How to secure socket connections with TLS/SSL (HTML)
- How to send and receive network data with a stream socket (HTML)
- How to set background connectivity options (HTML)
- How to set network capabilities (HTML)
- How to use advanced socket controls (HTML)
- Supporting proximity and tapping (HTML)
- Troubleshooting and debugging network connections
- Reference
- StreamSocket
- StreamSocketListener
- Windows.Networking
- Windows.Networking.Sockets
- Windows.Storage.Streams.DataReader
- Windows.Storage.Streams.DataWriter
- Samples
- DatagramSocket sample
- ControlChannelTrigger TCP socket sample
- Windows 8 app samples
Operating system requirements
| Client | |
|---|---|
| Server | |
| Phone |
Build the sample
- Start Visual Studio 2013 Update 2 and select File > Open > Project/Solution.
- Go to the directory to which you unzipped the sample. Go to the directory named for the sample, and double-click the Visual Studio 2013 Update 2 Solution (.sln) file.
- Follow the steps for the version of the sample you want:
-
To build the Windows version of the sample:
- Select StreamSocket.Windows in Solution Explorer.
- Press Ctrl+Shift+B, or use Build > Build Solution, or use Build > Build StreamSocket.Windows.
-
To build the Windows Phone version of the sample:
- Select StreamSocket.WindowsPhone in Solution Explorer.
- Press Ctrl+Shift+B or use Build > Build Solution or use Build > Build StreamSocket.WindowsPhone.
-
Run the sample
The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.
Deploying the sample
-
To deploy the built Windows version of the sample:
- Select StreamSocket.Windows in Solution Explorer.
- Use Build > Deploy Solution or Build > Deploy StreamSocket.Windows.
-
To deploy the built Windows Phone version of the sample:
- Select StreamSocket.WindowsPhone in Solution Explorer.
- Use Build > Deploy Solution or Build > Deploy StreamSocket.WindowsPhone.
Deploying and running the Windows version of the sample
For the app to attempt a socket connection using SSL to a web server at port 443 (HTTPS) and view the server certificate, this sample requires that a web server is available that supports HTTPS. The web server must be started before the app is run. The sample includes a PowerShell script that will install and enable IIS on a local computer, generate a self-signed, untrusted certificate, and enable HTTPS connections. The easiest way to run the sample is to use the provided web server scripts.
Browse to the Server folder in your sample folder to setup and start the web server. There are two options possible.
- Start PowerShell elevated (Run as administrator) and run the following command:
.\SetupServer.ps1
Note that you may also need to change script execution policy.
- Start an elevated Command Prompt (Run as administrator) and run following command:
PowerShell.exe -ExecutionPolicy Unrestricted -File SetupServer.ps1
When the web server is not needed anymore, please browse to the Server folder in you sample folder and run one of the following:
- Start PowerShell elevated (Run as administrator) and run the following command:
.\RemoveServer.ps1
Note that you may also need to change script execution policy.
- Start an elevated Command Prompt (Run as administrator) and run following command:
PowerShell.exe -ExecutionPolicy Unrestricted -File RemoveServer.ps1
The sample can run using any web server, not only the one provided with the sample. If IIS is used on a different computer, then the previous scripts can be used with minor changes.
- Copy the Server folder to the device where IIS will be run.
- Run the above scripts to install and enable IIS, generate a self-signed certificate, and enable HTTPS connections.
The sample must also be updated when run against a non-localhost web server. To configure the sample for use with IIS on a different device:
- Additional capabilities may need to be added to the app manifest for the sample. For example, Internet (Client & Server) if the web server is located on the Internet not on a local intranet.
- The hostname of the server to connect to also needs to be updated. This can be handled in two ways. The HostNameForConnect element in the HTML or XAML files can be edited so that "localhost" is replaced by the hostname or IP address of the web server. Alternately when the app is run, enter the hostname or IP address of the web server instead of the default "localhost" value.
Note IIS is not available on ARM builds. Instead, set up the web server on a separate 64-bit or 32-bit computer and follow the steps for using the sample against non-localhost web server.
However if a server different than IIS is used, then this requires some special configuration of the server.
- Configure the web server to accept HTTPS connections.
- Generate a self-signed certificate for the web server with SN=www.fabrikam.com.
To configure the sample for use with a web server different than IIS not using localhost:
- Additional capabilities may need to be added to the app manifest for the sample. For example, Internet (Client & Server) if the web server is located on the Internet not on a local intranet.
- The hostname of the server to connect to also needs to be updated. This can be handled in two ways. The HostNameForConnect element in the HTML or XAML files can be edited so that "localhost" is replaced by the hostname or IP address of the web server. Alternately when the app is run, enter the hostname or IP address of the web server instead of the default "localhost" value.
-
To deploy and run the Windows version of the sample:
- Right-click StreamSocket.Windows in Solution Explorer and select Set as StartUp Project.
- To debug the sample and then run it, press F5 or use Debug > Start Debugging. To run the sample without debugging, press Ctrl+F5 or use Debug > Start Without Debugging.
Deploying and running the Windows Phone version of the sample
IIS is not available on Windows Phone. For the app to attempt a socket connection using SSL to a web server, there are two options:
- The easiest way to run the sample is to use the provided web server scripts on a separate 64-bit or 32-bit device that can run IIS. Follow the instructions for deploying and running the Windows version of the sample using IIS on a different device.
- Use a web server different than IIS on a separate device. Follow the instructions for deploying and running the Windows version of the sample using a non-IIS web server.
-
To deploy and run the Windows Phone version of the sample:
- Right-click StreamSocket.WindowsPhone in Solution Explorer and select Set as StartUp Project.
- To debug the sample and then run it, press F5 or use Debug > Start Debugging. To run the sample without debugging, press Ctrl+F5 or use Debug > Start Without Debugging.