BitTorrent: The Protocol (2)
when we read the name “BitTorrent” suddenly a thought came into mind “why the name is “BitTorrent”??” Bit refers to binary data. (1 bit is the smallest unit of binary data). and torrent means the transmission of data from files like streaming.
Let's have a deeper look at what protocol consist of:
There are 5 main components of the BitTorrent protocol:
- Metainfo File: A file that consists of metadata of file shortly all necessary info of torrent file.
- Tracker: Server which helps to manage the BitTorrent protocol.
- Peers: The users who are exchanging the data with the help of the protocol.
- Data: main important our torrent works for is the transportation of data via BitTorrent protocol from one peer to another.
- Client: The client is the program that implements the protocol which is on the peer's computer.
Peers use TCP(Transport Control Protocol) for sending the data and communicating. TCP guarantees reliable and in-order delivery of data from sender to receiver. TCP is preferred over UDP cause it doesn't give such guarantees, and data can become scrambled, or lost altogether. Peers communicate with trackers with HTTP(Hypertext Transfer Protocol). Trackers responses to peers for their queries. Tracker tells peer which peer has which data. Peer establishes communication between peers. every peer is connected with trackers as a tracker allows to begin communication.
How two peers interact with other peers and tracker?


peers interact with another peer over TCP and peer interact with tracker via HTTP.
Chunks: Data splits into smaller chunks which sent between peers using the BitTorrent protocol. These chunks are of fixed size. Trackers keep info on who has which chunk of data.file s broken out in chunks, and each chunk is assigned to a hashcode, which can be checked by the downloader for data integrity. These hashcodes are part of the ‘metainfo file’.
size of chunks remains constant except the last one, which is the remaining part after broken chunks of equal size. These chunks allocated a definite memory so it will help download efficiently. if you assign more size to each then there is a larger risk of data corruption due to fewer integrity checks. if chunks are small then it will require more hashes so can take longer for hash checks. This hashcodes of every chunks are stored in a metainfo file. metainfo file not larger than 50–75kb. The main reason to keep this file small is hosting storage. generally, these chunks are of 256kb,512kb, and 1Mb.
no. of chunks = (total length/chunk size)
for example: 2.8MB file could be divided into 11 chunks.
10 chunks of 256 kb and last 1 chunk is of 240kb
256kb*10 + 240kb*1 = 2800 kb
The metainfo file: This file is all about information of a file any peer wants to download.If anyone wants to share the file via torrent then that peer should create the metainfo file. This file is specific to data they are publishing and information about torrents such as data to be included in the file and trackers address. This file is given .torrent extension and the data is extracted by the program which is the BitTorrent client on peers computer.
every metainfo file contains the following keys.
- Announce: The URL of the tracker. (As a string)
- Info: Dictionary which describes the files of a torrent. Hashes for chunks in SHA1 format, length of the file in bytes. simple dictionary for a single file and more files it's a directory structure.
some more optional keys:
- creation date: The creation time of the torrent. (integer seconds since 1-Jan-1970 00:00:00 UTC)
- comment: Any comments by the author
- created by: Name and Version of a program used to create the metainfo file
The keys contained in the metainfo file are encoded before they are sent. Encoding is done using BitTorrent specific method known as ‘bencoding’. Example of metainfo file( taken from wiki source of the torrent)
{
‘announce’: ‘http://bttracker.debian.org:6969/announce',
‘info’:
{
‘length’: 678301696,
‘name’: ‘Debian-503-amd64-CD-1.iso’,
‘piece length’: 262144,
‘pieces’: <binary SHA1 hashes>
}
}
Bencoding: Bencoding is used by BitTorrent for sending our metainfo file to the tracker from our client. Strings, Integers, lists, and dictionaries are supported by encoding. Bencoding uses starting delimiters ‘i’,'l’,‘d’ for integers, lists, dictionaries respectively. Ending delimiter is always ‘e’. there is no delimiter for byte strings.
Bencoding Structure:
Byte Strings : <string length in ASCII> : <string data>
Integers: i<ASCII base 10>e
Lists: l<bencoded values>e
Dictionaries: d<bencoded string><bencoded element>e
Examples of bencoding:
4:moon // represents the string “moon”
i3e // represents the integer “3”
l4:moon3:sune // represents the list of two strings: [“moon”,”sun”]
d4:moonl1:a1:bee // represents the dictionary {“moon” => [“a” , “b”] }
Metainfo File Distribution: All information needed for the file is in only one file i.e is a metainfo file so distribution is easy over the protocol. As the file gets replicated no. of peers increases. The most popular method of file distribution is using a public indexing site that hosts the metainfo files. the seed will upload their metainfo file on public indexing site and then peers can download a copy of the file over HTTP and participate in the torrent.
Thanks for reading! do comments, claps, and share!