0

Is it better to keep a TCP socket open for a long time, or re-establish connection frequently?

Let's take HTTP in a browser as an example. Is it better to establish a connection, make the HTTP request, and upon receiving a response, close it only to open a new one when we make a new request? Or: Should you keep the socket open for as long as that particular user is still browsing that specific site?

Stewart
  • 4,356
  • 2
  • 27
  • 59
  • 1
    Considering keep alive was implemented to HTTP to enhance performance, that should tell something. But it depends on the timescales. Opening TCP connection is slow. – Sami Kuhmonen Jan 31 '17 at 08:06
  • 1
    It depends, what do you mean by "frequently"? Multiple times per second? Once every day? And how much data are you going to transfer each time you would reconnect (if you go that route)? – Some programmer dude Jan 31 '17 at 08:06
  • 1
    HTTP pipelining was designed to do this - keep the TCP connection open instead of doing a connect for each element on the webpage. And even request several elements before receiving the first one requested, i.e. the actual pipelining. – Erik Alapää Jan 31 '17 at 08:06
  • 1
    Better to keep open for better performance. Though it's not that slow to re-establish a connection. – cshu Jan 31 '17 at 08:10
  • Possible duplicate of [Multiple TCP connections vs single connection](http://stackoverflow.com/questions/19400481/), [Design/Architecture: web-socket one connection vs multiple connections](http://stackoverflow.com/questions/31134420/), [TCP is it possible to achieve higher transfer rate with multiple connections?](http://stackoverflow.com/questions/259553/), [Multiple HTTP requests vs a single TCP connection on iOS](http://stackoverflow.com/questions/5655109/) and probably others. – Steffen Ullrich Jan 31 '17 at 08:15
  • Thanks all. My particular situation involves a potential for several messages per second, less than 1KB per second, but the application needs to run for weeks on end. I like the idea of keeping it open. I'll leave it open. Thanks – Stewart Feb 01 '17 at 20:46

1 Answers1

0

There is no problem with leaving a socket open. Keep alive option is meant for this. I'll leave it open.

Stewart
  • 4,356
  • 2
  • 27
  • 59