TCP Features: Connection-oriented and Reliable Networking

beginner
20 min

TCP Features: Connection-oriented and Reliable Networking

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.

What is TCP? 🎯

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.

Why Connection-oriented? 📝

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.

Establishing a Connection 💡

  1. Three-way Handshake: When a client wants to connect to a server, it sends a 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.
bash
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)
  1. Data Transfer: Once the connection is established, data can be sent back and forth between the client and server.

  2. 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.

bash
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)

Why Reliable? 📝

Reliable means that TCP ensures the delivery of data without errors, in the correct order, and with guaranteed delivery acknowledgement. TCP achieves this through:

  1. ACK (Acknowledgement) packets: After receiving data, the receiver sends an ACK packet back to the sender to confirm successful delivery.

  2. 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.

  3. Retransmission: If the receiver does not receive an ACK within a specific time frame, the sender will retransmit the packet.

  4. Flow Control and Congestion Avoidance: TCP uses flow control and congestion avoidance algorithms to manage the data transmission rate and prevent network congestion.

Practical Example: Simple TCP Server and Client ✅

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.

bash
# Server python3 tcp_server.py # Client python3 tcp_client.py

tcp_server.py and tcp_client.py are provided for you to experiment with and learn how TCP works in a real-world scenario.

Quick Quiz
Question 1 of 1

What does the Three-way Handshake establish in a TCP connection?

markdown
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.
python
# 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())