/TorrentClone

Creating a torrent clone that has an index server which communicates with the peers in UDP and the peers communicate with each other through TCP.

Primary LanguageC

TorrentClone

TLDR

A P2P application (created in C) that allows peers to share and download content through an index server via UDP and TCP communication.

Usage

  1. Make sure to compile the peer-sk and server-sk files (I used GCC 8.5.0)
  2. Run the server: ./server-sk <port_number>
  3. Run peer(s): ./peer-sk <port_number

Note: For demo purposes you can clone the peer-sk executable into different folders to emulate content sharing.

Technical Details

This torrent clone project consists of a P2P (Peer-to-Peer) application that allows clients to share and download content through an index server utilizing UDP for communication and TCP for content download. The peers act as clients to upload and download content and act as servers to allow other peers to view and download content, making the peer both a client and server for content. The index server facilitates these activities allowing the peers to exchange content amongst each other.

Socket Diagram

Figure 1: Socket Diagram

The peer to peer application consists of TCP and UDP socket connections. Both socket protocols can be used for all elements of this project, however the performance and reliability of each vary depending on the task. UDP is better suited for handling the peer connections as it is a connectionless service that allows multiple users to connect simultaneously. There is no acknowledgement sent with each message in UDP, which allows for quicker connections than TCP. However, UDP lacks in lossless data transfer as the acknowledgement messages in TCP connection protocol ensure no data is lost during transmission. As shown in the figure above, S1, S5, S6, S8 use TCP sockets for content download and S2, S3, S4, S7 use UDP sockets for index server and peer connections. Thus, UDP is used for peer to peer communication and TCP is used to handle content exchange.

UDP Connection

Figure 2: UDP Connection

The peer first establishes a connection with the index server using UDP socket connection allowing peers to become clients and communicate along the index server. To register a file, the peer will connect to the index server including the IP address of the peer and the socket port number alongside the content upload. These will be used by other peers to reference content on the server and allow registering and deregistering of content.

Peer description

General Code Description & Implementation method:

The client program was implemented using a base while loop that checked the commands that were being received. The client program retained a global “registeredContent” list to keep track of content that was being registered to the index server, as well as the corresponding TCP socket to allow the client to pose as a content server when certain content was being requested. Once this was established, it was only a matter of creating specific functions – to which their functions and descriptions are outlined below this paragraph – to meet the requirements of the client.

Table of PDU type with their corresponding function & descriptions:

PDU Type Function Direction
R
  • Accepts download requests by starting a passive TCP socket
  • Peer that acts as the content server registers its content and sends an R type PDU with peer name, content name, and port address to the index server
  • The address that is registered becomes the port number of the newly created TCP socket
  • If the peer is downloading content, sends an R type PDU to register the downloaded content and becomes the new server
Sending: peer to index server
D
  • TCP connection established with the content server and the address extracted with the S type PDU
Peer (content server) to peer (content client)
S
  • Contacts the index server to request the address of the content server that holds the requested content with an S type PDU containing peer name and content name
  • Extracts the address from the S type PDU that’s sent from the index server
Sending: peer (content server) to index server

Receiving: index server to peer (content client)

C
  • Transmits the requested content through the TCP connection
  • If the content is larger than 1000 bytes, the file will be sent through multiple packets
  • Content server terminates upon completion of the download
Sending: peer (content server) to peer (content client)
T
  • Peer makes a request to deregister its content from the server
Peer (content server) to index server)
Q
  • Peer makes a request to quit and deregister all its contents through T type PDUs
Peer (content server) to index server
O
  • Lists all registered content in the server upon user request
Sending: peer to index server

Receiving: index server to peer

Server Program Description

General Code Description & Implementation method:

The server was implemented using a while loop in the main method that waited for specific messages to be received from the client (via UDP), and based on the PDU type, executed certain functions. The server contains a global variable to retain the list of connections being made, and each connection structure would contain the peer name and a corresponding content linked list, with each entry depicting the content name, address, port, and a pointer to the next linked list entry. The table below outlines the pseudocode for each PDU type received by the server.

Table of PDU type with their corresponding function & descriptions:

PDU Type Function Direction
R
  • Performs a check to see if another peer with the same name has already registered content with the same file name
  • If no conflicts in user and content names exist, content is registered and associated with the address of the content server
Receiving: peer to index server

Peer becomes content server after registration

S
  • Searches of the address of the content server that the peer requests to download from and sends the address if found
  • Ensures that the address of the content server with the least interaction is sent
Sending: peer (content client) to index server

Receiving: index server to peer (content client)

T
  • Deregisters contents if requested by the peer
Peer (content server) to index server
O
  • Sends a list of all registered contents to the peer
Sending: peer to index server

Receiving: index server to peer

A
  • Sends an acknowledgement of successful registration to the peer
  • Sends an acknowledgement of successful deregistration to the peer
Index server to peer
E
  • Sends an error during content registration if two peers exist with the same name and content name
  • Sends an error when the search for an address of the desired content fails
Index server to peer