A simplified BitTorrent-like peer-to-peer file sharing system implemented in Java.
Peers connect to each other using TCP sockets and collaboratively download pieces of a file until every peer obtains the complete file.
Peers exchanging pieces of a file across the network.
The system implements core BitTorrent mechanisms such as:
- Piece-based file distribution
- Bitfield exchange
- Interested / Not Interested messages
- Request / Piece transfer
- Choking / Unchoking mechanism
- Preferred neighbor selection
- Optimistic unchoking
Each peer acts as both a client and a server:
- Server role: Accept incoming connections from other peers.
- Client role: Initiate connections to peers that started earlier.
Once connected, peers exchange information about the pieces they possess and request missing pieces from neighbors. The file is split into fixed-size pieces, which are downloaded and assembled locally by each peer.
project_root/
├── src/ # Java source files
├── build/ # Compiled .class files (generated after build)
├── Makefile # Build and run configuration
├── Common.cfg # Global configuration parameters
└── PeerInfo.cfg # Peer information configuration
javac -d ../build *.java
cd ../build
java PeerProcess 1001
make run PEER=1001Currently, peers must be started manually.
Example:
make run PEER=1001
make run PEER=1002
make run PEER=1003
Each peer reads configuration files and connects to other peers based on the order defined in PeerInfo.cfg.
Defines peers participating in the network.
1001 localhost 6001 0
1002 localhost 6002 1
1003 localhost 6003 0
Defines global parameters.
NumberOfPreferredNeighbors 3
UnchokingInterval 5
OptimisticUnchokingInterval 10
FileName thefile
FileSize 2167705
PieceSize 16384
The system follows a message-based protocol similar to BitTorrent.
- Handshake Peers establish TCP connections and exchange a handshake message containing peer identifiers.
- Bitfield Exchange Peers share a bitfield indicating which pieces they possess.
- Interest Determination Peers determine whether they are interested in pieces from another peer. Possible messages: - interested - not interested
- Choking / Unchoking
Peers regulate bandwidth by controlling which neighbors may request pieces.
Two mechanisms are used:
- Preferred neighbors
- Optimistic unchoking
- Piece Requesting Interested peers send request messages for missing pieces.
- Piece Transfer Peers send the requested data using piece messages.
- File Reconstruction Once all pieces are received, the peer reconstructs the complete file.
Every UnchokingInterval seconds:
- The peer calculates download rates from neighbors
- The top K peers become preferred neighbors
- Preferred neighbors are unchoked
- Others are choked.
Every OptimisticUnchokingInterval seconds:
- One randomly selected peer is optimistically unchoked.
This ensures fairness and prevents peers from being permanently choked.
The file is split into pieces of size PieceSize.
Each peer:
- Downloads pieces independently
- Stores pieces locally
- Updates its bitfield
- Broadcasts
havemessages - Reconstructs the file after receiving all pieces
Testing was performed in a local environment using:
localhostfor peer communication- Multiple peers running on different ports
- Java TCP socket communication The system can also run across multiple machines if hostnames and ports are configured accordingly.
