A complete implementation of the Transmission Control Protocol (TCP) built from scratch in Python.
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.
- 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
- 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
The project was developed incrementally across 9 stages:
- Stage 1: 3-way handshake implementation
- Stage 2: Basic data transmission and ACK handling
- Stage 3: Out-of-order packet buffering and reordering
- Stage 4: Sliding window protocol and flow control
- Stage 5: Window advertisement and updates
- Stage 6: Passive connection close
- Stage 7: Active connection close
- Stage 8: Packet retransmission with fixed timeouts
- Stage 9: Adaptive RTO using RTT estimation (RFC 6298)
StudentUSocket: Main TCP socket implementationTXControlBlock: Manages send sequence space and windowRXControlBlock: Manages receive sequence space and windowRetxQueue: Handles packet retransmission with timeoutsRecvQueue: Buffers out-of-order packets for reorderingFinControl: Manages connection teardown procedures
The implementation supports all standard TCP states:
CLOSED,SYN_SENT,ESTABLISHEDFIN_WAIT_1,FIN_WAIT_2,CLOSING,TIME_WAITCLOSE_WAIT,LAST_ACK
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 9Implements proper 32-bit sequence number comparisons with wraparound handling using modular arithmetic operators.
Dynamic calculation of retransmission timeout using:
- Smoothed RTT (SRTT)
- RTT variance (RTTVAR)
- Adaptive RTO with exponential backoff
Sophisticated handling of out-of-order packets with:
- Receive queue for buffering
- Gap detection and filling
- Duplicate elimination
- Python 3.x
- POX networking framework
- Custom modular arithmetic library
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()- Handles high packet loss rates with adaptive retransmission
- Efficient sliding window implementation for throughput
- Optimized for both latency and reliability
- Memory-efficient packet buffering
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
- RFC 793: Transmission Control Protocol
- RFC 6298: Computing TCP's Retransmission Timer
- RFC 1122: Requirements for Internet Hosts