CMSC 417 - Computer Networks - Spring 2026
- Java 17+ (tested with OpenJDK 21)
- Gradle 8.7+ (wrapper included)
./gradlew build# Basic usage
java -cp build/classes/java/main bittorrent.Main <torrent-file>
# With options
java -cp build/classes/java/main bittorrent.Main \
-o ./downloads \
--strategy rarest \
--choking standard \
flatland.torrent
# Or build a fat jar first
./gradlew jar
java -jar build/libs/bittorrent-client-1.0.jar flatland.torrent -o ./downloads| Flag | Values | Default | Description |
|---|---|---|---|
-o <dir> |
path | . |
Output directory |
--strategy |
sequential, rarest, endgame |
sequential |
Piece selection strategy |
--choking |
standard, bittyrant, propshare |
standard |
Choking algorithm |
-v |
- | off | Verbose logging |
-h |
- | - | Show help |
./gradlew test- HTTP tracker communication (hand-coded over raw sockets, no HTTP libraries)
- Compact peer format support
- Single announce URL and announce-list (BEP 12) support
- Download files from other instances of this client
- Download files from official BitTorrent clients (Transmission, qBittorrent)
- UDP tracker (BEP 15) - connect/announce over DatagramSocket
- HTTPS tracker - hand-coded HTTP wrapped with SSLSocketFactory
- Multi-file mode - pieces mapped across file boundaries
- Rarest-first strategy - piece availability counting, random tie-breaking
- Endgame mode - aggressive requesting when few pieces remain
- BitTyrant - minimize upload to maintain reciprocation
- PropShare - proportional upload allocation based on download received
bittorrent/
├── Main.java CLI entry point
├── bencode/ Bencode encoder/decoder
├── torrent/ .torrent file parser
├── tracker/ HTTP, HTTPS, UDP tracker clients
├── peer/ Wire protocol (handshake, messages, connections)
├── download/ Download orchestration, piece management, file I/O
└── strategy/ Pluggable piece selection and choking algorithms
- Threading: One thread per peer connection + scheduled executor for periodic tasks
- Thread safety:
synchronizedblocks on PieceManager,CopyOnWriteArrayListfor peer list,AtomicInteger/AtomicLongfor counters - Strategy pattern: Pluggable
PieceStrategyandChokingStrategyinterfaces - File I/O:
RandomAccessFilewith byte-range mapping for multi-file support - No third-party libraries: All HTTP/UDP communication hand-coded over raw sockets
- SHA-1:
java.security.MessageDigest(standard library)
40 tests across 7 test files covering all core and extra credit features:
| Test File | Tests | Coverage |
|---|---|---|
| BencodeTest | 7 | Encode/decode all types, round-trip, invalid input |
| TorrentFileTest | 6 | Metadata parsing, info hash, announce URLs, piece sizes |
| TrackerResponseTest | 3 | Compact peers, failure response, non-compact peers |
| PeerMessageTest | 5 | All message types, serialization, bitfield packing |
| PieceManagerTest | 6 | Piece lifecycle, block management, completion tracking |
| StrategyTest | 8 | Sequential, rarest-first, endgame, choking |
| HttpTrackerClientTest | 4 | URL encoding, construction, response parsing |