Skip to content

This project implements a fully functional TCP stack that handles reliable data transmission over unreliable networks. The implementation follows RFC 793 (TCP specification) and RFC 6298 (TCP retransmission timer) to provide robust network communication.

Notifications You must be signed in to change notification settings

navya448/TCP-implementation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TCP Implementation in Python

A complete implementation of the Transmission Control Protocol (TCP) built from scratch in Python.

Overview

This project implements a fully functional TCP stack that handles reliable data transmission over unreliable networks. The implementation follows RFC 793 (TCP specification) and RFC 6298 (TCP retransmission timer) to provide robust network communication.

Features

Core TCP Functionality

  • 3-Way Handshake: Complete connection establishment (SYN → SYN-ACK → ACK)
  • Reliable Data Transfer: Ensures all data arrives correctly and in order
  • Flow Control: Sliding window protocol with advertised window management
  • Out-of-Order Handling: Buffers and reorders packets that arrive out of sequence
  • Connection Teardown: Both active and passive close procedures
  • State Management: Full TCP state machine with 9 states

Advanced Features

  • Packet Retransmission: Automatic retransmission of lost packets
  • Adaptive Timeouts: Dynamic RTO calculation using smoothed RTT estimates
  • Sequence Number Arithmetic: Proper handling of 32-bit wraparound
  • Congestion Control: Basic window-based flow control

Implementation Stages

The project was developed incrementally across 9 stages:

  1. Stage 1: 3-way handshake implementation
  2. Stage 2: Basic data transmission and ACK handling
  3. Stage 3: Out-of-order packet buffering and reordering
  4. Stage 4: Sliding window protocol and flow control
  5. Stage 5: Window advertisement and updates
  6. Stage 6: Passive connection close
  7. Stage 7: Active connection close
  8. Stage 8: Packet retransmission with fixed timeouts
  9. Stage 9: Adaptive RTO using RTT estimation (RFC 6298)

Architecture

Key Components

  • StudentUSocket: Main TCP socket implementation
  • TXControlBlock: Manages send sequence space and window
  • RXControlBlock: Manages receive sequence space and window
  • RetxQueue: Handles packet retransmission with timeouts
  • RecvQueue: Buffers out-of-order packets for reordering
  • FinControl: Manages connection teardown procedures

State Machine

The implementation supports all standard TCP states:

  • CLOSED, SYN_SENT, ESTABLISHED
  • FIN_WAIT_1, FIN_WAIT_2, CLOSING, TIME_WAIT
  • CLOSE_WAIT, LAST_ACK

Testing

Each stage includes comprehensive test suites that validate:

  • Correct packet transmission and reception
  • Proper state transitions
  • Timeout and retransmission behavior
  • Edge cases like sequence number wraparound
  • Performance under packet loss conditions
# Run tests for specific stage
python autograder.py s1  # Stage 1 tests
python autograder.py s6  # Stage 6 tests

# Run all tests up to stage N
python autograder.py all 9

Technical Highlights

Sequence Number Arithmetic

Implements proper 32-bit sequence number comparisons with wraparound handling using modular arithmetic operators.

RTT Estimation (RFC 6298)

Dynamic calculation of retransmission timeout using:

  • Smoothed RTT (SRTT)
  • RTT variance (RTTVAR)
  • Adaptive RTO with exponential backoff

Packet Reordering

Sophisticated handling of out-of-order packets with:

  • Receive queue for buffering
  • Gap detection and filling
  • Duplicate elimination

Requirements

  • Python 3.x
  • POX networking framework
  • Custom modular arithmetic library

Usage

The TCP implementation integrates with the POX networking framework and can handle real network traffic simulation.

# Example usage (simplified)
socket = StudentUSocket(manager)
socket.connect(ip_address, port)
socket.send("Hello, TCP!")
data = socket.recv(1024)
socket.close()

Performance

  • Handles high packet loss rates with adaptive retransmission
  • Efficient sliding window implementation for throughput
  • Optimized for both latency and reliability
  • Memory-efficient packet buffering

Learning Outcomes

This project provided hands-on experience with:

  • Low-level network protocol implementation
  • Systems programming and debugging
  • Performance optimization techniques
  • RFC specification interpretation
  • Test-driven development methodology

References


About

This project implements a fully functional TCP stack that handles reliable data transmission over unreliable networks. The implementation follows RFC 793 (TCP specification) and RFC 6298 (TCP retransmission timer) to provide robust network communication.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages