How to create a permanent socket connection on Android?

First, let me say that do not hesitate to recommend me if long-lasting TCP persistent connections are the way or the permanent HTTP connection.

I also read in advance that instead of a permanent connection, I may have a polling mechanism.

I'm just asking in a curious interest, how can I create a permanent connection with Android on the server?

Thanks!

+4
source share
2 answers

Long-lived TCP connections are a bad idea in everything mobile, because the network is so spotty. Personally, I use UDP or transient HTTP connections with the concept of an HTTP session.

+2
source

It really depends on your requirements and if you really need a permanent connection or not.

If you have time-dependent data, you need to click from the server to the device as soon as it becomes available, then it will be better for you to maintain a constant TCP connection.

If it’s acceptable that your server and device only periodically exchange information, then choosing a poll or HTTP requests may be the best choice.

Personally, I believe that a well-implemented long-term TCP connection with a binary protocol is the best choice when working with persistent connections, where the information should always be up to date.

HTTP connections are usually expensive in terms of overhead for each packet, especially if you use an XML-based protocol such as SOAP. In addition, plugging and unplugging connectors all the time is usually quite expensive.

Permanent TCP connections, on the other hand, can be difficult to implement both on the client side and on the server side. Battery life is a huge factor on the device side, and if you expect something more than a few users connected right away, you may need to implement an asynchronous server-side communication model, which will cause your own set of problems.

Good luck

+2
source

Source: https://habr.com/ru/post/1308357/


All Articles