views:

212

answers:

4

Hi! I've checked out TCP protocol Wiki, but haven't found, if socket connection will time out if no data is transferred during the long period. I mean.. there will be no physical problems, but two computers will just have no data to send each other for some time. How connection will still exist? Will there be some low-level data transfers to help it understand it is not broken?

Thank you! Sorry if question is dumb..

A: 

Connections are often kept open using keepalives.

Noufal Ibrahim
Connections will always stay open. Keep alives do not stop the connections from closing, given the standard TCP protocol.
anon
Ibrahim, thanks for the link! So, connection will close after some time, right? (because by default keep-alive mechanism is off)
roma
Not as far as I can tell. It's just that TCP implementations can choose to implement a keepalive mechanism. You cannot rely on it being there.
Noufal Ibrahim
A: 

No it won't. If you need either side to disconnect of no data is transferred, you need to implement it yourself, using some sort of "keep alive" mechanism.

anon
Sorry, I haven't understood.. Will keep-alives help me to keep connection open or vice versa? Thank you for the answer.
roma
+2  A: 

A TCP connection will stay open until both ends close it.

Though be aware that firewalls and especially NAT gateways will often time out their TCP connections entries, meaning if your connection goes through one, sending data might error out if the gateway has removed the mapping for that TCP connection. Only reading from a TCP connection a gateway has timed out will not be detected unless you have some kind of heartbeat in your application protocol or enable tcp keepalive.

nos
I guess this answers my question clearly :) Thank you.As I understand, when firewall will close the connection, I'll just get socket error while trying to send data?
roma
Yes, you'll get some sort of error. It might take a while.There has been a bit of research done into the behaviour of NATs and firewalls, and the conclusion is that one packet every 30 seconds will keep your mappings open for essentially all connections that work in the first place.
Andrew McGregor
+1  A: 

No data is sent over the network to maintain TCP connections. You can send a network layer keep-alive simply by send()ing a zero byte packet from either peer or enable socket options to have the operating system periodically send them for you. In my opinion application layer keep-alives (your application protocol manages it) provide better design/relability than transport layer machinary.

When desinging application protocols layered on top of TCP for maximum reliability it is typically required that you incorporate some kind of non-operation (NOOP), ping, heartbeat within the protocol design.

This is very important because for example if your a server listening for a request from a client and the client is turned off after the connection is made the TCP session is essentially orphaned and your server may end up listening forever. Connection interruption cannot be detected if no data is ever sent or received!!

If the server at least sent noop/ping/heartbeats at regular intervals the outgoing request would trigger the TCP layer retrans/timeout machinary and the server would then be able to detect the dead connection. If instead your application sends an application layer "ping" or "hi, how are you?" message you can go further and use it to inquire about the state of your peer rather than simply the underlying connection.

For example if a peer is stuck in an infinite loop or its disk drives are on fire TCP keepalives alone don't help you understand and retact to the underlying problem.

Einstein
Thank you for so many details, it really enlightened me)
roma