question

Madhu-8808 avatar image
0 Votes"
Madhu-8808 asked ·

Stream Sockets

Hi,

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?


Thanks,
Madhu



windows-uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichardZhang-MSFT avatar image
1 Vote"
RichardZhang-MSFT answered ·

Hello,​

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.

Thanks!

· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much for the detailed explanation. In the above scenario it'll be one way. But we'll also have scenarios where student want to send something to the teacher in certain situations. In that case can we still use StreamSocket for two way communication?

0 Votes 0 ·

Yes, this is certainly possible, StreamSocket is a pipeline that allows data to flow in both directions.

1 Vote 1 ·
yanxiaodi avatar image
1 Vote"
yanxiaodi answered ·

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.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.