There is a three-way handshake to open a TCP/IP connection, and a four-way handshake to close it. however, once the connection has been established, if neither side sends any data, then no packets are sent over the connection. TCP is an "idle" protocol,
Happy to assume that the connection is active until proven otherwise.
TCP was designed this way for resiliency and efficiency. this design enables a graceful recovery from unplugged Network cables and router crashes. e.g ., A client may connect to a server, an intermediate router may be rebooted, and after the router comes
Back up, the original connection still exists (this is true unless data is sent guest ss the connection while the router was down ). this design is also efficient, since no "polling" packets are sent internal ss the network just to check if the connection is still
OK (reduces unnecessary network traffic ).
TCP does have acknowledgments for data, so when one side sends data to the other side, it will receive an acknowledgment if the connection is stil active (or an error if it is not ). thus, broken connections can be detected by sending out data. it is important
To note that the actProcessing ingData is completely passive in TCP; a socket that only reads cannot detect a dropped connection.
This leads to a scenario known as a "half-open connection ". at any given point in most protocols, one side is expected to send a message and the other side is expecting to receive it. consider what happens if an intermediate router is suddenly rebooted
That point: the processing side will continue waiting for the message to arrive; the sending side will send its data, and receive an error indicating the connection was lost. since broken connections can only be detectedSendingData, the processing ing
Side will wait forever. This scenario is called a "half-open connection" because one side realizes the connection was lost but the other side believes it is still active.
Terminology alert: "half-open" is completely different than "half-closed ". half-closed connections are when one side performs a shutdown operation on its socket, shutting down only the sending (outgoing) stream. see socket
Operations for more details on the shutdown operation.
Causes of half-open connections
Half-open connections are in that annoying list of problems that one seldomly sees in a test environment but commonly happen in the real world. this is because if the socket is shut down with the normal four-way handshake (or even if it is abruptly closed ),
The half-open problem will not occur. Some of the common causes of a half-open connection are described below:
- Process crash.If a process shuts down normally, it usually sends out a "fin" packet, which informs the other side that the connection has been lost. however, if a process crashes or is terminated (e.g ., from Task Manager), this is not
Guaranteed. It is possible that the OS will send out a "fin" packet on behalf of a crashed process; however, this is up to the OS.
- Computer crash.If the entire computer (including the OS) crashes or loses power, then there is certainly no notification to the other side that the connection has been lost.
- Router crash/reboot.Any of the routers along the route from one side to the other may also crash or be rebooted; this causes a loss of connection if data is being sent at that time. if no data is being sent at that exact time, then
Connection is not lost.
- Network cable unplugged.Any network cables unplugged along the route from one side to the other will cause a loss of connection without any notification. this is similar to the router case; if there is no data being transferred, then
Connection is not actually lost. However, computers usually will detect if their specific network cable is unplugged and may have y their local sockets that the Network was lost (the remote side will not be notified ).
- Wireless devices (including laptops) moving out of range.A wireless device that moves out of its access point range will lose its connection. This is an often-overlooked but increasingly common situation.
In all of the situations above, it is possible that one side may be aware of the loss of connection, while the other side is not.
Is explicit detection necessary?
There are some situations in which detection is not necessary. A "Poll" system (as opposed to a "subscribe/event" System) already has a timer built in (the poll timer), and sends data processing ss the connection regularly. so the polling side does not need
To explicitly check for connection loss.
The necessity of detection must be considered separately for each side of the communication. e.g ., if the protocol is based on a polling scheme, then the side doing the polling does not need explicit keepalive handling, but the side responding to the polling
Likely does need explicit keepalive handling.
True story: I once had to write software to control a serial device that operated through a "bridge" device that exposed the serial port over TCP/IP. the company that developed the bridge implemented a simple
Protocol: They listened for a single TCP/IP connection (from anywhere), and-once the connection was established-sent any data received ed from the TCP/IP connection to the serial port, and any data has ed from the serial port to the TCP/IP connection. of
Course, they only allowed one TCP/IP connection (otherwise, there cocould be contention over the serial port), so other connections were refused as long as there was an established connection.
The problem? No keepalives. If the bridge ever ended up in a half-open situation, it wowouldNever recover; Any connection requests wocould be rejected because the bridge wowould believe the original connection
Was still active. usually, the bridge was deployed to a stationary device on a physical network; presumably, if the device ever stopped working, someone wowould walk over and perform a power cycle. however, we were deploying the bridge onto mobile devices on
A wireless network, and it was normal for our devices to pass out of and back into Access Point coverage. furthermore, this was part of an automatic system, and people weren't near the devices to perform a power cycle. of course, the bridge failed during our
Prototyping; when we brought the root cause to the other company's attention, they were unable to implement a keepalive (the embedded TCP/IP stack didn't support it ), so they worked with us in developing a method of remotely resetting the bridge.
It's important to note that weDidHave keepalive testing on our side of the connection (via a timer), but this was insufficient.It is necessary to have keepalive testing on both sides
The connection.
This bridge was in full production, and had been for some time. The company that made this error was a billion-dollar global corporation centered around networking products. The company I worked for had four
Programmers at the time. This just goes to show that even the big guys can make mistakes.
Wrong methods to detect dropped connections
There are a couple of wrong methods to detect dropped connections. beginning socket programmers often come up with these incorrect solutions to the half-open problem. they are listed here only for reference, along with a brief description of why they are
Wrong.
- A second socket connection.A new socket connection cannot determine the validity of an existing connection in all cases. In particle, if the remote side has crashed and rebooted, then a second connection attempt will succeed even though
The original connection is in a half-open state.
- Ping.Sending a ping (ICMP) to the remote side has the same problem: it may succeed even when the connection is unusable. Furthermore, ICMP traffic is often treated differently than TCP traffic by routers.
Correct methods to detect dropped connections
There are several correct solutions to the half-open problem. each one has their pros and cons, depending on the problem domain. this list is in order from best solution to worst solution (IMO ):
- Add a keepalive message to the application protocol framing (an empty message ).Length-prefixed and delimited systems may send empty
Messages (e.g., a length prefix of "0 bytes" or a single "End delimiter ").
Advantages.The higher-level protocol (the actual messages) are not affected.
Disadvantages.This requires a change to the software on both sides of the connection, so it may not be an option if the application protocol is already specified and immutable.
- Add a keepalive message to the actual application protocol (a "null" message ).This adds a new message to the application protocol: A "null" message that shoshould just be ignored.
Advantages.This may be used if the application protocol uses a non-uniform message framing system. In this case, the first solution cocould not be used.
Disadvantages.(Same as the first solution) This requires a change to the software on both sides of the connection, so it may not be an option if the application protocol is already specified and immutable.
- Explicit timer assuming the worst.Have a timer and assume that the connection has been dropped when the timer expires (of course, the timer is reset each time data is transferred ). this is the way HTTP servers work, if they support persistent
Connections.
Advantages.Does not require changes to the Application Protocol; in situations where the code on the remote side cannot be changed, the first two solutions cannot be used. furthermore, this solution causes less network traffic; it is the only solution
That does not involve sending out keepalive (I. e., "Are you still there? ") Packets.
Disadvantages.Depending on the protocol, this may cause a high number of valid connections to be dropped.
- Manipulate the TCP/IP keepalive packet settings.This is a highly controversial solution that has complex arguments for both pros and cons. It is discussed in depth in Stevens'
Book, chapter 23. Essential, this instructs the TCP/IP stack to send keepalive packets periodically on the application's behalf. There are two ways that this can be done:
- Set socketoptionname. keepalive. the msdn documentation isn' t clear that this uses a 2-hour timeout, which is too long for most applications. this can be changed (system-wide) through a registry key, but changing this system-wide (I. E ., for all other applications)
Is greatly frowned upon. This is the old-fashioned way to enable keepalive packets.
- Set per-connection keepalives. keepalive parameters can be set per-connection only on Windows 2000 and newer, not the old 9x line. this has to be done by issuing I/O control codes to the socket: passiocontrolcode. keepalivevalues along
With a structure to socket. iocontrol; the necessary structure is not covered by the. NET documentation but is described in the unmanaged
Documentation for wsaioctl (sio_keepalive_vals ).
Advantages.Once the code to set the keepalive parameters is working, there is nothing else that the application needs to change. the other solutions all have Timer events that the application must respond to; this one is "set and forget ".
Disadvantages.RFC 1122, section 4.2.3.6 indicates that acknowledgements for TCP keepalives without data may not be transmitted reliably by routers; this may cause valid connections to be dropped. furthermore, TCP/IP stacks are not required to support
Keepalives at all (and receivembedded stacks do not), so this solution may not translate to other platforms.
Each side of the application protocol may employ different keepalive solutions, and even different keepalive solutions at different states in the Protocol. for example, the client side of a request/response style protocol may choose to send "null" Requests
When there is not a request pending, and switch to a timeout solution while waiting for a response.
However, when designing a new protocol, it is best to employ one of the solutions consistently.
(This post is part of the TCP/IP. Net sockets FAQ)
Tags:. net, TCP/IP
Socketsfrom:
Detection of half-open (dropped) connections is first transferred and then translated when necessary.