TCP provides an effective abstraction of a reliable network running over an unreliable channel,
hiding most of the complexity of network communication from our applications:
retransmission of lost data,
in-order delivery
, congestion control and avoidance
, data integrity, and more. When you work with a TCP stream, you are guaranteed that all bytes sent will be identical with bytes received and that they will arrive in the same order to the client
three-way handshake
SYN
Client picks a random sequence numberx
and sends a SYN packet, which may also include additional TCP flags and options.
SYN ACK
Server incrementsx
by one, picks own random sequence numbery
, appends its own set of flags and options, and dispatches the response.
ACK
Client increments bothx
andy
by one and completes the handshake by dispatching the last ACK packet in the handshake.
The client can send a data packet immediately after the ACK packet, and the server must wait for the ACK before it can dispatch any data.