A P2P application (created in C) that allows peers to share and download content through an index server via UDP and TCP communication.
- Make sure to compile the peer-sk and server-sk files (I used GCC 8.5.0)
- Run the server:
./server-sk <port_number>
- 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.
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.
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.
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.
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 |
|
Sending: peer to index server |
D |
|
Peer (content server) to peer (content client) |
S |
|
Sending: peer (content server) to index server
Receiving: index server to peer (content client) |
C |
|
Sending: peer (content server) to peer (content client) |
T |
|
Peer (content server) to index server) |
Q |
|
Peer (content server) to index server |
O |
|
Sending: peer to index server
Receiving: index server to peer |
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 |
|
Receiving: peer to index server
Peer becomes content server after registration |
S |
|
Sending: peer (content client) to index server
Receiving: index server to peer (content client) |
T |
|
Peer (content server) to index server |
O |
|
Sending: peer to index server
Receiving: index server to peer |
A |
|
Index server to peer |
E |
|
Index server to peer |