I
I
Igor Che2017-02-01 08:39:25
linux
Igor Che, 2017-02-01 08:39:25

Socket server for multiple clients with a persistent connection. How?

I am making a socket server to which hundreds of clients should be connected with a persistent connection. The client is a device with sensors. which periodically sends statuses. Those. the amount of information is small. At the beginning of the connection, greetings are exchanged to make sure that it was not Uncle Vasya who knocked. Then statuses are sent periodically.
About sockets I read almost enough. The thing is pretty primitive. Very helpful in understanding this and this article. But some questions still remain.

1. Is a permanent connection possible?
The article at the second link says this:

But if you plan to reuse your socket for further transfers, you need to realize that there is no EOT on a socket. I repeat: if a socket send or recv returns after handling 0 bytes, the connection has been broken. If the connection has not been broken, you may wait on a recv forever, because the socket will not tell you that there's nothing more to read (for now).

I understand correctly that in order for the connection to be permanent, you need to continuously exchange packets?

2. The server must be asynchronous. What is better to use for this without unnecessary complication?
- threads
- asyncio
- socketserver
- twisted
- tornado

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alex, 2017-02-09
@nesterwsx

1. Perhaps see TCP keepalive. It is necessary to correctly handle the socket of a failed client.
2. nginx.org/ru/#architecture_and_scalability + www.kegel.com/c10k.html It is
better to read Stevens about sockets.

R
Roman Vasilenko, 2017-02-09
@farewell

Are you sure you need sockets?
If I understand correctly, clients only send their state to the server. In this case, if the frequency is low (more than 10 seconds), it might be better not to keep a constant connection, but to do everything through requests.

M
marataziat, 2018-05-27
@marataziat

Pubnub was made for you!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question