Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice: Keep TCP/IP connection open or close it after each transfer?

My Server-App uses a TIdTCPServer, several Client apps use TIdTCPClients to connect to the server (all computers are in the same LAN).

Some of the clients only need to contact the server every couple of minutes, others once every second and one will do this about 20 times a second.

If I keep the connection between a Client and the Server open, I'll save the re-connect, but have to check if the connection is lost.

If I close the connection after each transfer, it has to re-connect every time, but there's no need to check if the connection is still there.

What is the best way to do this?

At which frequency of data transfers should I keep the connection open in general?

What are other advantages / disadvantages for both scenarios?

like image 964
Holgerwa Avatar asked Feb 17 '11 16:02

Holgerwa


3 Answers

I would suggest a mix of the two. When a new connection is opened, start an idle timer for it. Whenever data is exchanged, reset the timer. If the timer elapses, close the connection (or send a command to the client asking if it wants the connection to remain open). If the connection has been closed when data needs to be sent, open a new connection and repeat. This way, less-often-used connections can be closed periodically, while more-often-used connections can stay open.

like image 73
Remy Lebeau Avatar answered Oct 07 '22 01:10

Remy Lebeau


Two Cents from experiment...

My first TCP/IP client/server application was using a new connection and a new thread for each request... years ago...

Then I discovered (using ProcessExplorer) that it consummed some network resources because all closed connection are indeed not destroyed, but remain in a particular state for some time. A lot of threads were created...

I even had some connection problems with a lot of concurent requests: I didn't have enough ports on my server!

So I rewrote it, following the HTTP/1.1 scheme, and the KeepAlive feature. It's much more efficient, use a small number of threads, and ProcessExplorer likes my new server. And I never run out of port again. :)

If the client has to be shutdown, I'll use a ThreadPool to, at least, don't create a thread per client...

In short: if you can, keep your client connections alive for some minutes.

like image 40
Arnaud Bouchez Avatar answered Oct 07 '22 00:10

Arnaud Bouchez


While it may be fine to connect and disconnect for an application that is active once every few minutes, the application that is communicating several times a second will see a performance boost by leaving the connection open.

Additionally, your code will be much simple if you aren't trying to constantly open, close, or diagnose an open connection. With the proper open and close logic, and SEH around your read and writes, there's no reason to test if the socket is still connected before using, just use it. It will tell you when there is a problem.

I'd lean towards keeping a single connection open in most enterprise applications. It generally will lead to cleaner code, that is easier to maintain.

/twocents

like image 33
mbelles Avatar answered Oct 07 '22 00:10

mbelles