lingtoshigure / torrent-client-cpp

A simple BitTorrent client written in C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Torrent Client C++

This is a simple BitTorrent client written in C++. The code and the general structure of the program are primarily based on the following blog posts:

I have found both posts in this repository, which contains numerous tutorials. It is definitely worth looking into for anyone who wants to implement their own tools from ground up.

Note that this is my very first C++ project, which I mainly started to familiarize myself with the language. Hence, the code might not comply with the normal C++ styles, and there are undoubtedly places where the code can be optimized.

Quick start

If you want to try this BitTorrent client yourself, follow the steps below:

  • Clone this repository, and make sure that cmake (> 3.19.2) is installed.
  • Type the following commands into the terminal (note that fetching the cpr library will take some time, so the compilation process might be time-consuming.):
$ cd torrent-client-cpp && mkdir build && cd build
$ cmake .. && make -j
  • To download the ubunto iso as an example, enter the following command:
$ ./BitTorrentClient -t ../res/ubuntu-12.04.5-alternate-amd64.iso.torrent -o ../res/  # logging disabled
$ ./BitTorrentClient -t ../res/ubuntu-12.04.5-alternate-amd64.iso.torrent -o ../res/  -l # logging enabled, log can be found in logs/client.log
  • The output will be similar to this:
Parsing Torrent file ../res/ubuntu-12.04.5-alternate-amd64.iso.torrent...
Download initiated...
[Peers: 4/5, 0.52 MB/s, ETA: 25:18][>                                       ] 4/1522 [0.26%] in 00:13
  • All files in the res directory can be used for testing. The files MoralPsychHandbooks.torrent and ComputerNetworks.torrent all come from Academic Torrents. They are small Torrent files which are suitable for testing all the basic functionalities of the BitTorrent client. Whereas the Torrent files for ubuntu and debian are great for stress tests.

Commandline Options

The program supports the following commandline options:

Options Alternative Description Default
-t --torrent-file Path to the Torrent file REQUIRED
-o --output-dir The output directory to which the file will be downloaded REQUIRED
-n --thread-num Number of downloading threads to use. (i.e maximum number of peers that the client can connect to) 5
-l --logging Enable logging false
-f --log-file Path to the log file ../logs/client.log
-h --help Print arguments and their descriptions

Supported Features

The current implementation of this BitTorrent client only supports the following features:

  • Retrieving a list of peers from the tracker periodically.
  • Downloading single-file Torrents in a multi-threaded manner.

To make it an actual usable BitTorrent client, it will have to include:

  • Seeding
  • Resuming a download.
  • Downloading multi-file Torrents
  • Probably a more intuitive user interface.
  • Pipelining when requesting blocks from peers.
  • Connecting to as many peers as possible.

Since this is only a project that I started for fun and to learn C++, it is unlikely that any of the unsupported features will be implemented any time soon. If you wish to build on top of my current code, feel free to do so.

About

A simple BitTorrent client written in C++

License:MIT License


Languages

Language:C++ 99.2%Language:CMake 0.6%Language:C 0.2%