A modern, feature-rich BitTorrent client built with C# and WPF, implementing the full BitTorrent protocol specification.
- C# 11 / .NET 8.0 β Core language and runtime
- WPF β Desktop UI framework (Windows only)
- MVVM β UI architecture pattern
- System.Net.Sockets β TCP networking for peer/tracker communication
- System.Net.Http β HTTP/HTTPS tracker requests
- System.IO β File and stream management
- System.Threading β Async operations and concurrency
- Custom Bencode Parser β For .torrent files and tracker responses
- Custom Peer Wire Protocol Implementation β For BitTorrent peer messaging
- AI/Heuristic Peer Selection β Adaptive, extensible peer ranking
- Logging β To file and console for debugging and troubleshooting
- StealthMode β Privacy features, peer ID masking, and tracker request obfuscation
- Full BitTorrent Protocol Support: Complete implementation of BEP 3 (BitTorrent Protocol)
- Torrent File Parsing: Parse and load .torrent files with metadata extraction
- Tracker Communication: HTTP/HTTPS tracker support with peer discovery
- Peer Wire Protocol: Full peer-to-peer communication with piece management
- Modern WPF UI: Clean, intuitive interface with real-time progress tracking
- Console Mode: Command-line interface for testing and automation
- File Management: Support for both single and multi-file torrents
- Piece Verification: SHA1 hash verification for data integrity
- Progress Tracking: Real-time download progress, speed, and ETA
- Peer Management: Connection management and peer statistics
- .NET 8.0 SDK or later
- Windows 10/11 (for WPF UI)
- Visual Studio 2022 or later (recommended)
git clone <repository-url>
cd BitTorrent-ClientUsing PowerShell (recommended):
.\build.ps1Using Batch file:
build.batUsing dotnet CLI:
dotnet restore
dotnet build --configuration ReleaseWPF Mode (GUI):
dotnet run --project BitTorrentClientConsole Mode (for testing):
dotnet run --project BitTorrentClient -- --console- Add Torrent: Click "Add Torrent" and select a .torrent file
- Select Download Location: Choose where to save the files
- Start Download: Click "Start" to begin downloading
- Monitor Progress: View real-time progress, speed, and peer information
- Control Downloads: Use Start/Stop/Pause/Remove buttons to manage torrents
The console mode provides a simple interface for testing:
- Enter the path to a .torrent file
- Specify the download directory
- Monitor real-time progress in the console
- Press any key to stop the download
TorrentParser.cs: Parses .torrent files and extracts metadataBencodeParser.cs: Handles Bencode encoding/decoding (BEP 3)TrackerClient.cs: Communicates with BitTorrent trackersPeerConnection.cs: Manages peer-to-peer connectionsPieceManager.cs: Handles piece verification and file assemblyTorrentDownloader.cs: Orchestrates the entire download process
MainWindow.xaml: Main application windowTorrentViewModel.cs: MVVM view model for torrent managementApp.xaml: Application entry point
The client includes an AI-powered peer selection module (Core/AI/IntelligentPeerSelector.cs) that optimizes which peers to connect to and request pieces from. This module:
- Scores and ranks peers based on download speed, reliability, response time, and piece success rate.
- Promotes or demotes peers dynamically, blacklisting unreliable ones and prioritizing high-performing peers.
- Adapts peer selection in real time to maximize download speed and reliability.
- Extensible for future ML integration (e.g., reinforcement learning for peer selection).
This intelligent selection helps the client avoid slow or malicious peers and improves overall torrent performance.
- β BEP 3: BitTorrent Protocol
- β Bencode: Data serialization format
- β Peer Wire Protocol: Message types and handshaking
- β Tracker Protocol: HTTP/HTTPS tracker communication
- β Piece Management: Download, verification, and assembly
- β Choking Algorithm: Basic peer selection
- π Magnet Links: Planned (BEP 9)
- π DHT: Planned (BEP 5)
choke(0): Peer is choking the clientunchoke(1): Peer is unchoking the clientinterested(2): Client is interested in peernot_interested(3): Client is not interested in peerhave(4): Peer has a piecebitfield(5): Peer's piece availabilityrequest(6): Request a piece blockpiece(7): Piece block datacancel(8): Cancel a request
BitTorrentClient/
βββ Core/ # Core protocol implementation
β βββ TorrentParser.cs # Torrent file parsing
β βββ TrackerClient.cs # Tracker communication
β βββ PeerConnection.cs # Peer wire protocol
β βββ PieceManager.cs # Piece management
β βββ Downloader.cs # Main download orchestrator
βββ ViewModels/ # MVVM view models
β βββ TorrentViewModel.cs # Torrent management
βββ Tests/ # Unit tests
β βββ TorrentParserTests.cs
βββ MainWindow.xaml # Main UI
βββ App.xaml # Application entry
βββ Program.cs # Program entry point
-
Install Prerequisites:
- .NET 8.0 SDK
- Visual Studio 2022 (optional)
-
Clone Repository:
git clone <repository-url> cd BitTorrent-Client
-
Restore Dependencies:
dotnet restore
-
Build Project:
dotnet build --configuration Release
-
Run Tests:
dotnet test
The project is designed with extensibility in mind:
- New Protocol Features: Add to the Core namespace
- UI Enhancements: Modify WPF files in the root
- Additional Formats: Extend the parser classes
Run the test suite:
dotnet test-
Console Mode: Test core functionality
dotnet run --project BitTorrentClient -- --console
-
WPF Mode: Test user interface
dotnet run --project BitTorrentClient
For testing, you can use:
- Ubuntu ISO torrents (legal and reliable)
- Open source software distributions
- Public domain content
-
Build Errors:
- Ensure .NET 8.0 SDK is installed
- Run
dotnet restorebefore building - Check for missing dependencies
-
Network Issues:
- Verify firewall settings
- Check port 6881 availability
- Ensure tracker URLs are accessible
-
Permission Errors:
- Run as administrator if needed
- Check download directory permissions
Enable detailed logging:
dotnet run --project BitTorrentClient --configuration Debug- The client listens on port 6882 by default (previously 6881). You can change this in
TorrentDownloaderandStealthModeif needed.
- The client writes debug logs to
debug.login the working directory and also outputs logs to the console. - If you do not see the log file, check the console output or search your user folder for
debug.log. - Log messages include
[DEBUG],[PEER], and[DOWNLOADER]for detailed protocol and connection information.
If you see Connected Peers: 0/0 and no download progress:
- Check the console for
[PEER]and[DOWNLOADER]log messages. - Make sure your firewall allows inbound and outbound connections on port 6882.
- Try a different torrent with a healthy swarm.
- If all peer connections time out, the tracker may be returning stale or unreachable peers.
- The log file may not update if the app is run from a different working directory. Try searching your user folder for
debug.log.
- Some ISPs block BitTorrent ports (6881-6889). Try changing the port if you have connectivity issues.
- The log file may not update if the app is run from a different working directory.
- The client currently does not support DHT or magnet links (planned features).
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- BitTorrent Protocol Specification (BEP 3)
- .NET Community for excellent tooling
- Open source community for inspiration
- Magnet link support (BEP 9)
- Distributed Hash Table (BEP 5)
- Peer Exchange (BEP 11)
- IPv6 support
- Bandwidth limiting
- Encryption (BEP 7)
- Web seed support (BEP 19)
- Cross-platform UI (Avalonia)