TCP
TCP = Transmisson Control Protocol
How TCP starts and closes session?
Three stages of TCP
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
Three-way handshake to start a session
- Client sends a single
SYN
packet to the server, asking for a session
Client: Hi, server, do you want to talk?
Server replies with a
SYNACK
packet (The server acknowledges the client’s request, and ask client for a talk)Client: Hi, server, do you want to talk?
Server: Yes, I want to talk. Do you want to talk?
The client replies with
ACK
packet.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.
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
The Server sends
FINACK
packet to the client.Server: I am done. Can you hear me?
The client responsed with
ACK
package.Server: I am done. Can you hear me?
Client: Yes, I got your message, I can hear you.
When the client completes download in the webpage, it sends
FINACK
to the serverServer: 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?
The server responses with
ACK
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.
The client sends
SYN
segment to the server, asking for synchronization (synchronization means connection)The server replies with
SYN-ACK
(synchronization and acknowledgement)- The server acknowledges the client’s connection request
- The server also asks the client to open a connection too.
The client replies
ACK
, which is like “Yes”. Then the two-way connection is established between them.
More Technical View
The client sends a
SYN
segment with the initial sequence number9001
ACK
is set to 0SYN
is set to 1
The server replies with
SYN-ACK
- 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
- The server’s
The client responses
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 | |
---|---|---|
Reliable | Yes | No |
Connection | connection-oriented | connectionless |
Character | reliable | faster, more efficient |
Usage | dominant transport protocol |
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.
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.
UDP: connectionless
- 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)
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
- 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
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.
Now frame 2 is acknowledged.
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:
Reference: Sliding Window Protocol