TCP

TCP(Transmission Control Protocol) provides a connection-oriented, reliabled, byte stream service. So broadcasting and multicasting aren't applicable to TCP.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
APP(byte stream)
    ||
  	||                User Space
   	||           ---------------------
    ||                Kernel Space
	\/
TCP(segment)
    ||
	||
	||          TCP segment data field max size: MSS
	||          MSS <= MTU-40(IPv4)/MTU-60(IPv6)
	||
	\/
IP(datagram)
    ||
	||
	||          IP datagram max size: MTU
	||          MTU is defined by Hardware of Network
	||
	\/
Hardware and Driver

TCP Header

| IP header(20B) | TCP header(20~60B) | TCP data |

TCP header(20~60B):

  • source port number(2B)
  • destination port number(2B)
  • sequence number(4B): order in segment sequence
  • acknowledgement number(4B): next sequence number expects to receive
  • (2B)
    • header length(4bit): length of 4B, so the max size of TCP header is 60B
    • reserved(6bit)
    • URG: urgent pointer is valid
    • ACK: acknowledgement number is valid
    • PSH: pass this data to app as soon as possible
    • RST: reset the connection
    • SYN: initiate a connection, use sequence number field for initial sequence number(ISN)
    • FIN: the sender is finished sending data
  • window size(2B)
  • TCP checksum(2B): for TCP header and TCP data
  • urgent pointer(2B): used by TCP's urgent mode
  • options(0~40B): set maximum segment size(MSS) with SYN when create a connection

socket pair: {Server IP, Server Port, Client IP, Client Port}

TCP Connection Establishment and Termination

Establishment

three-way handshake:

  1. The client sends a SYN segment, normally including:
    • port number of server in destination port number field
    • initial sequence number(ISN) of client in sequence number field
    • max segment size(MSS) of client in option field
  2. The server responds a SYN/ACK segment, including:
    • initial sequence number(ISN) of server in sequence number field
    • acknowlegement number for client SYN segment in acknowlegment number field
    • max segment size(MSS) of server in option field
  3. The client responds a ACK segment, including:
    • acknowlegement number for server SYN segment in acknowlegment number field
1
2
3
1. Client  ====(SYN c, mss)=============>  Server    (active open)
2. Client  <===(SYN s, ACK c+1, mss)=====  Server    (passive open)
3. Client  ====(ACK s+1)================>  Server

TCP provides a full-duplex service so each end of a connection must maintain a sequence number of data flowing in each direction. There are two different initial sequence numbers.

Termination

Since a TCP connection is full-duplex, each direction must be shutdown independently. A TCP can still send data after receiving a FIN(half-close).

  1. first end send a FIN segment
  2. second end responds a ACK segment, including:
    • acknowlegement number plus one for FIN segment in acknowlegment number field
  3. second end send a FIN segment
  4. first end responds a ACK segment, including:
    • acknowlegement number plus one for FIN segment in acknowlegment number field
1
2
3
4
5
1. Client  ====(FIN)=====>  Server
2. Client  <===(ACK)======  Server

3. Client  <===(FIN)======  Server
4. Client  ====(ACK)=====>  Server

The application could calls shutdown() instead of callingclose to terminate one side connection.

Some Details

Maximum Segment Size(MSS)

MSS is max size for data field for tcp segment. Default MSS is 536B.

And for an Ethernet, MTU is 1500, so the best MSS is 1460.

TIME_WAIT

Maximum segment lifetime(MSL) is the maximum amount of time any segment can exist in the network before being discarded. When TCP performs an active close, and sends the final ACK, that connection must stay in the TIME_WAIT state for twice the MSL. So TIME_WAIT state is also called the 2MSL wait state(2 minutes x 2).

When connection is in TIME_WAIT state, socket pair for the connection cannot be reused. The connection can only be reused when the TIME_WAIT wait is over. This is not a problem for clients because clients use ephemeral ports. But server use well-known ports. It may take from 1 to 4 minutes before the server can be restarted.

If specify the SO_REUSEADDR option for socket, the socket will be reused the same port but only if the server does the active close.

Reference

  • TCP/IP v1