Stream Sockets

Madhu 96 Reputation points


We have two different UWP apps used in our project. From one App to the other we need to send some notification for a particular event. The 2 apps will always be running in 2 devices. App1 will be used by certain set of users in some devices and App 2 will be used by another set of users in another set of devices. When a user of App1 does some action we need to notify the App2.
We can assume always both the Apps will be running when that happens.
For e.g. In a classroom the teacher is using App 1 and the students are using the App2, during the class when all of them are using the App, teacher will send some notification to the student apps, so they need to get notified.

We are planning to use StreamSocket for the above task. So we'll create a Server (which listens for a connection) and a Client (which connects to the server).

When we create a socket connection like that, can we send messages from the server to client and client to server both ways? or only from the server to client?

If we create a connection like that, will it get timed out after a certain time? Does the Stream socket connection time out automatically or will last until we close it?

If it doesn't time out and if we keep it running continuously, can that affect the battery life of tablets badly?


Universal Windows Platform (UWP)
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,936 Reputation points


    Welcome to our Microsoft Q&A platform!

    From the description of your application scenario, it is necessary to use the server as a data transfer station.

    But from your description, the data is transmitted in one direction, that is, the teacher sends a notification to the student, but does not need to receive feedback from the student.

    So you can use this process:

    The student device establishes a connection with the server and keeps listening -> the teacher sends a request to the server -> the server sends information to all connected devices

    After you create a StreamSocket, if there is no packet transmission for a long time, the connection will be disconnected. If you want to stay connected for a long time, you can set StreamSocketListener.KeepAlive to True (the default is False).

    In addition, StreamSocket may be abnormally closed in the following cases:

    When the StreamSocket connection target is out of range

    When a file IO error occurs while reading Stream

    When the application crashes off

    More infomation in this document

    In order to ensure the application experience, it is recommended to manually close StreamSocket, such as at rest.

    I believe this design is to ensure the battery life and reduce the resource consumption, so you don't have to worry about creating a StreamSocket that will affect the battery life of the device.


    1 person found this answer helpful.

  2. Xiaodi Yan 876 Reputation points MVP

    Hi, socket connection supports dual-way communication between the server and the client.
    You could use TCP or UDP for your scenario.

    A TCP (Transmission Control Protocol) socket provides low-level network data transfers in either direction for connections that are long-lived. A UDP (User Datagram Protocol) socket is similar to a TCP socket in that it also provides low-level network data transfers in either direction. But, while a TCP socket is for long-lived connections, a UDP socket is for applications where an established connection is not required. Find more details here: Sockets in UWP

    @RichardZhangWicresoftNorthAmericaL-8157 has explained how to keep a long-lived connection and when it will be disconnected. You are able to keep a long-live socket connection but that means your applications are always running on the foreground so it definitely will affect the battery life. In this case, you could delegate ownership of a socket to a system socket broker. The broker will activate your app when traffic arrives on the socket. Then it will transfer the ownership back to your app so your app then processes the arriving traffic. More details are available here: Network communications in the background

    Not sure what your specific scenario is but if you need to make sure the communication is reliable, you should use TCP. Otherwise, you could use TCP but there is no guarantee for the reliability.

    1 person found this answer helpful.
    0 comments No comments