TCP

TCP = Transmisson Control Protocol

How TCP starts and closes session?

Three stages of TCP

截屏2021-03-15 11.43.05

  1. Session starting
  2. Data transmission
  3. Session ending

This three stages make TCP a connect-oriented and reliable protocol. 👏

Suppose a client wants to get web pages from a server. Three stages below will be gone through.

Session Starting

截屏2021-03-15 11.31.50

Three-way handshake to start a session

  1. Client sends a single SYN packet to the server, asking for a session
截屏2021-03-15 11.13.10

Client: Hi, server, do you want to talk?

  1. Server replies with a SYNACK packet (The server acknowledges the client’s request, and ask client for a talk)

    截屏2021-03-15 11.27.59

    Client: Hi, server, do you want to talk?

    Server: Yes, I want to talk. Do you want to talk?

  2. The client replies with ACK packet.

    截屏2021-03-15 11.30.19

    Client: Hi, server, do you want to talk?

    Server: Yes, I want to talk. Do you want to talk?

    Client: Yes, I want to talk.

Data Transmission

After three-way handshake, connection is established. And data packets are going to be transferred.

截屏2021-03-15 11.35.37

During data transmission, TCP also guarantees that data is successfully received and resembled in a correct order.

Session Ending

After server sends all packets to the client, a four-steps procedure is performed before the connection is closed

  1. The Server sends FINACK packet to the client.

    截屏2021-03-15 11.38.15

    Server: I am done. Can you hear me?

  2. The client responsed with ACK package.

    截屏2021-03-15 11.39.06

    Server: I am done. Can you hear me?

    Client: Yes, I got your message, I can hear you.

  3. When the client completes download in the webpage, it sends FINACK to the server

    截屏2021-03-15 12.11.00

    Server: I am done. Can you hear me?

    Client: Yes, I got your message, I can hear you.

    Client: I am done. Can you hear me?

  4. The server responses with ACK

    截屏2021-03-15 11.41.18

After this, the session between them can be properly close, unless the client continues to ask for another webpage.

TCP Three-way Handshake in Detail

Suppose the client wants to get web pages from the server. Before any web page transmission, TCP connection must be established through three-way handshake.

  1. The client sends SYN segment to the server, asking for synchronization (synchronization means connection)

    截屏2021-03-15 12.42.14
  2. The server replies with SYN-ACK (synchronization and acknowledgement)

    截屏2021-03-15 12.43.43
    • The server acknowledges the client’s connection request
    • The server also asks the client to open a connection too.
  3. The client replies ACK, which is like “Yes”. Then the two-way connection is established between them.

3_time_handshake

More Technical View

  1. The client sends a SYN segment with the initial sequence number 9001

    • ACK is set to 0
    • SYN is set to 1

    截屏2021-03-15 12.50.33

  2. The server replies with SYN-ACK

    截屏2021-03-15 12.52.59

    • The server’s SYN is set to 1
    • ACK is set to 9002, which is the client’s sequence number plus 1.
      • By adding 1 to the client’s sequence number, the server simply acknowledges the client connection request)
    • The server’s segment has its own initial sequence number 5001
  3. The client responses

    截屏2021-03-15 12.56.22

    • SYN is set to 0 : There is NO more synchronization/connection request)
    • ACK is set to 5002: The client acknowledges the server connection request by increasing the server-side sequence number by 1 (5001 + 1 = 5002)
    • Seq is set to 9002: This is the second segment issued by the client at this point (9001 + 1 = 9002)

At this point, both client and server have agreed to open their connection to each other.

  • Step 1 and 2: establish the connection from client to server
  • Step 2 and 3: establish the connection from server to client

$\rightarrow$ Two-way connection channel is established and they are ready to exchange their messages.

TCP vs. UDP

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are two protocols of the Transport layer (Layer 4) in the OSI model.

TCP_UDP
TCPUDP
ReliableYesNo
Connectionconnection-orientedconnectionless
Characterreliablefaster, more efficient
Usagedominant transport protocol
  • live streaming audio/video
  • DNS queries, DHCP or VoIP
  • Only a few applications use UDP
  • Reliablity

    • When TCP delivers data segments to destination, the protocol makes sure

      • each segement is received,
      • no error,
      • and segments are put together in a correct order

      $\rightarrow$ TCP is reliable 👍

    • When UDP delivers data segments to destination, it does NOT guarantee, or even NOT care if the data reach the destination. Once the data is sent off, UDP says “Goodbye, and good luck!”

      $\rightarrow$ UDP is NOT reliable 🤪

    Connection

    TCP: connection-oriented

    • TCP uses three-way handshake to make sure the connection is established before data transmission.
    截屏2021-03-15 12.02.03
    • After data is delivered, TCP will follow a 4-step procedure to make sure every bit of data is delivered and received before clossing the connection.

      截屏2021-03-15 12.02.30

    UDP: connectionless

    截屏2021-03-15 12.05.16

    • No handshake to establish the connection
    • No procedure to close the connection

    Example for Understanding TCP and UDP

    TCP walks to a bar

    TCP: I want a beer.

    Bartender: You want a beer?

    TCP: Yes, I want a beer.

    UDP walks to a bar

    UDP: I want a beer. (He does NOT care if the bartender hears him or not)

    UDP might never get a beer…Well, he’s UDP. He doesn’t care.

    TCP Details

    Round Trip Time (RTT)

    round_trip_time

    Stop-and-Wait ARQ Protocol

    • After transmitting one frame, the sender waits for an acknowledgement before transmitting the next frame
    • If the acknowledgement does NOT arrive after a certain period of time, the sender times out and retransmits the original frame
    截屏2021-04-01 15.47.23
    • Drawbacks
      • One frame at a time
      • Poor utilization of bandwidth
      • Poor performance

    Sliding Window Protocol

    • Send multiple frames at a time
    • Number of frames to be sent is based on Window size
      • Window size = # frames that can be sent before expecting ACK
    • Each frame is numbered (called sequence number)

    Example

    • The sender wants to send 11 frames (Frame 0 to 10)

    • Window size is set to 4

    The receiver sends 4 frames at the same time

    截屏2021-04-01 15.50.54

    Now the receiver sends back an ACK for frame 0. Once frame 0 is acknowledged, the sender can send frame 4. Now look at the sliding window, it slides a little bit to the left.

    截屏2021-04-01 15.52.17

    Now frame 2 is acknowledged.

    截屏2021-04-01 15.55.17

    The process works similarly. Frame 0 and 1 is acknowledged. Frame 2 - 5 are in the sliding window, meaning that they’re already sent, but not acknowledged.

    Summary:

    截屏2021-04-01 15.58.26

    Reference: Sliding Window Protocol

    Reference

    How TCP starts and close session?

    Three-time handshake

    TCP vs. UDP