IP is a connectionless protocol, which means that each unit of data is individually addressed and routed from the source device to the target device, and the target does not send an acknowledgement back to the source.
That’s where protocols such as TCP come in. TCP is used in conjunction with IP in order to maintain a connection between the sender and the target and to ensure packet order.

3-way Handshake


  1. C2S: “Synchronize”, “SYN” with sequence number . We do this rather than assume 0 for security and reliability reason (no overlay window).
  2. S2C: “Synchronize and Acknowledge”, “SYN/ACK”. “SYN” with a sequence number and set the “ACK” bit to 1, ACK . which indicate the passive side has acknowledged that it received the “SYN”
  3. C2S: “Acknowledge”, “ACK” . Send a 0-length .

Connection Teardown

  1. A -> B: FIN, seq , ACK
  2. B -> A: (Data+) ACK , continue sending unfinished packets
  3. B -> A: FIN, seq , ACK
  4. A -> B: ACK
  • Problems with closed socket

    • What if final ack is lost in the network?
    • What if the same port pair is immediately reused for a new connection?
  • Solution:“active” closer goes into TIME WAIT

    • Active close is sending FIN before receiving one
    • Keep socket around for 2MSL (twice the “maximum segment lifetime“)
  • Can pose problems with servers

    • OS has too many sockets in TIMEWAIT, slows things down
    • Hack: Can send RST and delete socket, set SO_LINGER socket option to time O
    • OS won’t let you re-start server because port still in use (SO_REUSEADDR option lets you re-bind used port number)

TCP Header