A Python implementation of a reliable transport protocol built on top of UDP, featuring TCP-like reliability mechanisms including sliding window, timeout/fast retransmission, and error detection.
URP (UDP-based Reliable Protocol) provides reliable, in-order data delivery over the unreliable UDP protocol. This implementation includes:
- Connection Management: 2-way handshake (SYN/ACK) for setup, 1-way close (FIN/ACK) for teardown
- Reliable Transfer: Sliding window protocol with pipelining (configurable window size)
- Error Recovery: Timeout-based retransmission and fast retransmit (3 duplicate ACKs)
- Error Detection: 16-bit checksum (TCP/UDP-style one's complement)
- Network Simulation: Packet Loss and Corruption (PLC) module for testing
- Comprehensive Logging: Real-time event logging with detailed statistics
Sender Receiver
┌─────────────────┐ ┌─────────────────┐
│ File Reader │ │ File Writer │
├─────────────────┤ ├─────────────────┤
│ Sliding Window │ │ Receive Buffer │
│ Send Buffer │ │ Out-of-order │
│ Timer Manager │ │ Segment Handler │
├─────────────────┤ ├─────────────────┤
│ PLC Module │ │ │
│ (Loss/Corrupt) │ │ │
├─────────────────┤ ├─────────────────┤
│ UDP Socket │ ←──────────────→ │ UDP Socket │
└─────────────────┘ └─────────────────┘
urp_v3/
├── src/urp_v3/
│ ├── sender.py # Sender implementation with sliding window
│ ├── receiver.py # Receiver implementation with buffering
│ ├── segment.py # URP segment format (6-byte header)
│ ├── seqnum.py # 16-bit sequence number with wraparound
│ ├── plc.py # Packet Loss/Corruption simulator
│ ├── logger.py # Event logging and statistics
│ └── constant.py # Constants and state definitions
├── tests/ # Unit and integration tests
├── INTRO.md # Detailed design documentation (1400+ lines)
└── README.md # This file
# Create and activate virtual environment
python -m venv .venv
# Windows (bash/Git Bash)
source .venv/Scripts/activate
# Windows (CMD)
.venv\Scripts\activate.bat
# Windows (PowerShell)
.venv\Scripts\Activate.ps1
# Linux/macOS
source .venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Install package in development mode
pip install -e .# Run all tests
pytest -v
# Run specific test
pytest tests/test_integration_subprocess.py -vTerminal 1 - Start Receiver:
python -m urp_v3.receiver 56007 59606 output.txt 4000
# Args: receiver_port sender_port output_file max_winTerminal 2 - Start Sender:
python -m urp_v3.sender 59606 56007 input.txt 4000 100 0.1 0.05 0.1 0.05
# Args: sender_port receiver_port input_file max_win rto flp rlp fcp rcpParameters:
max_win: Maximum window size in bytes (e.g., 4000)rto: Retransmission timeout in milliseconds (e.g., 100)flp: Forward loss probability 0-1 (e.g., 0.1 = 10%)rlp: Reverse loss probability 0-1fcp: Forward corruption probability 0-1rcp: Reverse corruption probability 0-1
6-byte header structure:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number (16) |Reserved(13)|A|S|F| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum (16) | Payload (0-1000 bytes) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Sequence Number: 16-bit with modular arithmetic
- Flags: ACK, SYN, FIN (only one can be set at a time)
- Checksum: 16-bit one's complement (TCP/UDP-style)
- MSS: 1000 bytes maximum payload
Sender States:
CLOSED → SYN_SENT → ESTABLISHED → CLOSING → FIN_WAIT → CLOSED
Receiver States:
CLOSED → LISTEN → ESTABLISHED → TIME_WAIT → CLOSED
- Sliding Window: Sender maintains window of unACKed segments
- Cumulative ACKs: Receiver ACKs next expected byte number
- Timeout Retransmission: Single timer for oldest unACKed segment
- Fast Retransmit: Triggered by 3 duplicate ACKs
- Out-of-order Buffering: Receiver buffers future segments
- TIME_WAIT: 2-second wait state for delayed packets
Both sender and receiver generate detailed logs:
sender_log.txt example:
snd ok 0.00 SYN 1234 0
rcv ok 102.34 ACK 1235 0
snd ok 103.45 DATA 1235 1000
rcv ok 105.67 ACK 2235 0
...
Original data sent: 3500
Total data sent: 5500
Timeout retransmissions: 5
Fast retransmissions: 0
receiver_log.txt example:
rcv ok 0.00 SYN 1234 0
snd ok 0.12 ACK 1235 0
rcv ok 103.50 DATA 1235 1000
snd ok 103.55 ACK 2235 0
...
Original data received: 3500
Total data received: 5500
Duplicate segments received: 2
The project includes comprehensive tests:
- Unit tests: Checksum, sequence number arithmetic, segment encoding
- Integration tests: Full sender-receiver communication with various parameters
- Stress tests: High packet loss/corruption scenarios
# Run all tests
pytest -v
# Run with coverage
pytest --cov=urp_v3 --cov-report=htmlSender: Main sender class with state machine and window managementReceiver: Main receiver class with buffering and ACK generationSegment: Segment encoding/decoding and checksum calculationSequenceNumber: 16-bit sequence number with wraparound supportPLC: Packet loss and corruption simulatorLogger: Event logging and statistics tracking
- State Machine: Both sender and receiver use explicit state machines
- Observer: Logger observes and records all protocol events
- Strategy: PLC module applies configurable loss/corruption strategies
- INTRO.md - Comprehensive design document with:
- Protocol specification details
- Architecture diagrams
- Implementation strategy
- Testing plan
- Development phases
MIT License