Supports listening for an incoming network connection using a TCP stream socket or Bluetooth RFCOMM.
var streamSocketListener = new Windows.Networking.Sockets.StreamSocketListener();
The StreamSocketListener class has these types of members:
The StreamSocketListener class has these constructors.
|StreamSocketListener||Creates a new StreamSocketListener object.|
The StreamSocketListener class has these events.
|ConnectionReceived||An event that indicates that a connection was received on the StreamSocketListener object.|
The StreamSocketListener class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.
|BindEndpointAsync||Starts a bind operation on a StreamSocketListener to a local hostname and a local service name.|
|BindServiceNameAsync(String)||Starts a bind operation on a StreamSocketListener to a local service name.|
|BindServiceNameAsync(String, SocketProtectionLevel)||Starts a bind operation on a StreamSocketListener to a local service name with a specified SocketProtectionLevel to set on any bound sockets.|
|BindServiceNameAsync(String, SocketProtectionLevel, NetworkAdapter)||Starts a socket bind operation on a StreamSocketListener to a local service name on a specified network adapter with a specified SocketProtectionLevel to set on any bound sockets.|
|Dispose [C#, VB]||Performs tasks associated with freeing, releasing, or resetting unmanaged resources.|
The StreamSocketListener class has these properties.
|Read-only||Gets socket control data on a StreamSocketListener object.|
|Read-only||Gets socket information for the StreamSocketListener object.|
The StreamSocketListener class supports listening for an incoming network connection using a TCP stream socket and accepting the connection.
The typical order of operations is as follows:
- Create the StreamSocketListener.
- Use the Control property to retrieve a StreamSocketListenerControl object and set the socket quality of service required.
- Assign the ConnectionReceived event to an event handler.
- Call the BindServiceNameAsync or BindEndpointAsync method to bind to a local TCP port or service name.
- When a connection is received, use the StreamSocketListenerConnectionReceivedEventArgs object to retrieve the Socket property with the StreamSocket object created.
- Use the StreamSocket object to send and receive data.
- Call the Close method to stop listening for and accepting incoming network connections and release all unmanaged resources associated with the StreamSocketListener object. Any StreamSocket objects created when a connection is received are unaffected and can continue to be used as needed.
You must write code to handle exceptions when you call asynchronous methods on the StreamSocketListener class. Exceptions can result from parameter validation errors, name resolutions failures, and network errors. Exceptions from network errors (loss of connectivity, connection failures, and server failures, for example) can happen at any time. These errors result in exceptions being thrown. If not handled by your app, an exception can cause your entire app to be terminated by the runtime.
The Windows.Networking.Sockets namespace has a convenient helper method and enumeration for handling errors when using sockets. This can be useful for handling specific network exceptions differently in your app. An app can also use the HRESULT from the exception on parameter validation errors to learn more detailed information on the error that caused the exception.
For more information on possible exceptions and how to handle exceptions, see Handling exceptions in network apps.
On Windows Server 2012 and Windows Server 2012 R2, the Windows.Networking.dll that implements most of the classes in the Windows.Networking.Sockets namespace will fail to load unless the Media Foundation feature is enabled. As a result, apps that use StreamSocketListener and related socket classes in the Windows.Networking.Sockets namespace will fail if the Media Foundation feature is disabled. Windows Server 2012 or Windows Server 2012 R2 installs with the Media Foundation feature disabled.
The Media Foundation feature can be enabled on Windows Server 2012 or Windows Server 2012 R2 using Server Manager or by entering the following text in a command prompt or a script:
dism /online /enable-feature /featurename:ServerMediaFoundation
After the Media Foundation feature is enabled, the user is prompted to restart. Once the computer is restarted, classes for sockets and WebSockets in the Windows.Networking.Sockets namespace will work as expected.
Minimum supported client
|Windows 8 [Windows Store apps, desktop apps]|
Minimum supported server
|Windows Server 2012 [Windows Store apps, desktop apps]|
Minimum supported phone
|Windows Phone 8|
- Other resources
- Connecting to network services (Windows Store apps using C#/VB/C++ and XAML)
- Handling exceptions in network apps
- How to use advanced socket controls (Windows Store apps using C#/VB/C++ and XAML)
- Troubleshoot and debug network connections
Build date: 11/16/2013