Welcome to our deep dive into TCP (Transmission Control Protocol)! In this tutorial, we'll explore the two key features that make TCP an essential part of modern networking - its connection-oriented nature and its reliability.
By the end of this lesson, you'll understand how TCP works, its importance, and you'll get a hands-on experience with some practical examples.
TCP is a connection-oriented, reliable protocol that ensures the reliable transmission of data between two devices over the internet. It's a fundamental part of the Internet Protocol Suite (TCP/IP) and is used by applications to establish and maintain communication over a network.
Connection-oriented simply means that before data can be sent, a connection must be established between the sender and the receiver. This two-way communication channel provides a guaranteed path for data transmission.
SYN (synchronize sequence numbers) packet. The server replies with a SYN-ACK (synchronize-acknowledgement) packet, and the client responds with an ACK (acknowledgement) to complete the handshake.Client --> SYN (Sequence Number: x)
Server <-- SYN-ACK (Sequence Number: y, Acknowledgement Number: x+1)
Client --> ACK (Sequence Number: y+1, Acknowledgement Number: x+1)Data Transfer: Once the connection is established, data can be sent back and forth between the client and server.
Four-way Handshake (TCP Finish): When either the client or server wants to close the connection, it sends a FIN (finish) packet. The other side responds with a ACK to acknowledge the request, and the connection is closed.
Client --> FIN (Sequence Number: z, Acknowledgement Number: y)
Server <-- ACK (Sequence Number: z, Acknowledgement Number: z+1)
Client --> ACK (Sequence Number: y+1, Acknowledgement Number: z+1)
Server <-- FIN (Sequence Number: w, Acknowledgement Number: z+1)
Client <-- ACK (Sequence Number: w+1, Acknowledgement Number: z+1)Reliable means that TCP ensures the delivery of data without errors, in the correct order, and with guaranteed delivery acknowledgement. TCP achieves this through:
ACK (Acknowledgement) packets: After receiving data, the receiver sends an ACK packet back to the sender to confirm successful delivery.
Sequence and Acknowledgement Numbers: Each packet sent includes a sequence number that identifies its position in the data stream, and an acknowledgement number that confirms the last packet received.
Retransmission: If the receiver does not receive an ACK within a specific time frame, the sender will retransmit the packet.
Flow Control and Congestion Avoidance: TCP uses flow control and congestion avoidance algorithms to manage the data transmission rate and prevent network congestion.
Now that we've covered the basics, let's dive into a practical example of a simple TCP server and client. In this example, we'll build a server that accepts connections from clients and sends a greeting message.
# Server
python3 tcp_server.py
# Client
python3 tcp_client.pytcp_server.py and tcp_client.py are provided for you to experiment with and learn how TCP works in a real-world scenario.
What does the Three-way Handshake establish in a TCP connection?
Let's continue our journey into the world of TCP. In the next section, we'll discuss some advanced features of TCP and explore more practical examples to help reinforce your understanding.# tcp_server.py
import socket
HOST = '127.0.0.1'
PORT = 50007
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data.upper())
# tcp_client.py
import socket
HOST = '127.0.0.1'
PORT = 50007
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
while True:
message = input('Enter a message: ')
if message.lower() == 'quit':
break
s.sendall(message.encode())
response = s.recv(1024)
print('Received:', response.decode())