A complete BitTorrent client implementation in C++20 with multi-peer downloading, SHA-1 verification, and cross-platform support.
Click above β VS Code opens in browser β Run: ./run.sh docker
Two ways to run:
- Docker (instant, works anywhere)
- Local Build (for development)
Works on any system with Docker installed.
git clone https://github.com/tanmaygithub04/Bit-Torrent.git
cd Bit-Torrent./run.sh dockerThat's it! The script will:
- Build the Docker image automatically
- Download a sample torrent file
- Show real-time progress
- Save the file to
downloads/directory
For development and customization.
Ubuntu/Debian:
sudo apt-get update
sudo apt-get install build-essential libboost-all-dev libssl-dev cmakemacOS:
brew install boost openssl cmakegit clone https://github.com/tanmaygithub04/Bit-Torrent.git
cd Bit-Torrent
./run.sh buildThe script will compile and test automatically.
Bit-Torrent/
βββ src/
β βββ core/
β β βββ DownloadManager.cpp # Orchestrates downloads
β β βββ DownloadManager.h
β β βββ peer.cpp # Peer communication
β β βββ peer.h
β β βββ torrent.cpp # Torrent metadata parsing
β β βββ torrent.h
β β βββ tracker.cpp # Tracker communication
β β βββ tracker.h
β βββ utils/
β β βββ bencode.cpp # Bencode parsing
β β βββ bencode.h
β β βββ error.h # Error handling
β β βββ hash.cpp # SHA-1 hashing
β β βββ hash.h
β β βββ terminal_ui.h # UI utilities
β βββ Main.cpp # Entry point
βββ torrents/
β βββ sample.torrent # Sample torrent file
βββ downloads/ # Downloaded files appear here
βββ README.md # Project documentation
βββ run.sh # Simple runner script
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Main Application β
βββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββ
β Download Manager β
β β’ Orchestrates download process β
β β’ Manages piece verification β
β β’ Handles file assembly β
βββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ
β
ββββββββββββββββββΌβββββββββββββββββ
β β β
ββββββββββΌβββββββββ ββββββΌβββββββββ ββββ-βΌββββββββββ
β Torrent Parser β β Tracker β β Peer Managerβ
β β’ Metadata β β β’ Get peersβ β β’ Handshake β
β β’ Bencode β β β’ Announce β β β’ Messages β
β β’ Info hash β β β’ HTTP β β β’ Downloads β
βββββββββββββββββββ βββββββββββββββ ββββββββββββββββ
-
Block-Based Piece Downloadingβ : Downloads pieces in 16 KiB blocks by sending REQUEST messages and assembling the corresponding PIECE messages.
-
Piece Integrity Verificationβ : Computes the SHA-1 hash of each downloaded piece and compares it against the expected hash from the torrent metadata to ensure file integrity.
-
File Assemblyβ : Once all pieces are successfully downloaded and verified, the client assembles them into the final output file.
-
Peer Connection & Handshakeβ : Uses Boost.Asio to resolve peer addresses, establish TCP connections, and perform the BitTorrent handshake.
-
Torrent Metadata Parsingβ : Extracts essential information (info_hash, piece length, total file length, concatenated SHA-1 hashes) from torrent files.
-
Bencode Decodingβ : Implements bencode parsing utilities to correctly read and interpret torrent metadata.
-
Modular Architectureβ : Refactored from a monolithic implementation into separate, maintainable components (TorrentMetadata, Peer, and DownloadManager) to improve clarity and scalability.
Assumptions π This is a simple Bit torrent client without Piece Selection , or Tracker Implementation , or any Choking Algorithm so might not work for all torrent files The client assumes the .torrent file is valid and well-formed. Peers listed in the torrent metadata are available and active. The download directory (src/downloads/) exists before running the program.
- Make it compatibility with standard multi file torrents
- Can go in direction of video downloading/streaming or in the direction of implementing advanced algorithms
- Multithreaded piece downloading
- DHT (Distributed Hash Table) support
- Magnet link support
- Web UI interface
- Bandwidth throttling
-
Monolithic Code Structure Initially, all functionality was implemented in a single file, making the code hard to maintain. This necessitated refactoring into modular components. Linker and Undefined Symbols
-
Some functions were declared but not defined or had signature mismatches, leading to linker errors. URL Encoding and Conversion Errors
-
Improper encoding of binary data (like the info hash) resulted in conversion errors such as βstoul: no conversion.β UTFβ8 Validation Issues
-
The trackerβs compact peer data wasnβt valid UTFβ8, which broke JSON parsing until UTFβ8 validation was disabled. File Path and Directory Problems
-
Incorrect relative paths (e.g., using "../downloads/sample.txt" instead of the correct path) and missing directories led to file-opening errors. Piece Assembly Errors
-
Using vector insertion to assemble piece data appended blocks rather than placing them at the correct offsets, causing hash mismatches. State Management for Peer Communication
-
Waiting for an UNCHOKE message on every piece downloadβeven when the peer was already unchokedβresulted in failed downloads. Segmentation Faults & Memory Issues
-
Mismanagement of memory and null pointer dereferences caused segmentation faults during handshake and piece download. And Many more π
If you want to contribute:
- Fork the repo
- Create a feature branch
- Make changes and submit a PR π
This project is licensed under the MIT License - see the LICENSE file for details.
Demo β’ Documentation β’ Issues
Built with C++20 β’ Boost.Asio β’ OpenSSL β’ Docker