Zealand => Technique=> exercise
Simple Peer to Peer

Updated : 2020-10-19

Exercise: Simple Peer to Peer

The idea of this exercise is to better understand the Peer-2-Peer systems (Computer Network chap 2 s.168-175 (and see wiki: peer2peer). 
This architecture differs from the 'normal' client-server (see wiki: client-server).  


Simple P2P is distributed system.

Simple P2P consists of a centralized registry database, along with several  'peer's. They are communicating through a protocol in three steps (referring to the figure below):

  1. When a new Peer Server would like to use the Simple P2P, It have to registry its own files in a centralized unit (a registration database),
    Then this centralized register have all knowledge of filenames and where to find them i.e. ip-addresses and port numbers. 
  2. When a Peer Client would like to download a file, the Peer client have to search in the central register to get a list of ip-addresses together with the port number of Peer-servers who provide this file.
  3. Now the Peer Client have to pick one from the list and then set up a connection to the Peer Server holding this file for downloading.
figure of P2P communication
Figure from Kurose & Ross

Logical view of the system

Logical view  

Be aware of all three elements can be implemented on the same or on different computers and the two Peers could (but not necessary) be implemented in the same program.

Description of the three elements  

The Three elements

Registry server

This is the central Registry Database, which contains pairs of filename and belonging addresses ( Make a class 'FileEndPoint' with an ip-address as a string and a port-number ) of Peers.
You should consider a data structure for holding a filename (a key) and a set of addresses (this could be a Dictionary-structure).
The Registry Server must be implemented as a REST-service.

REST service interface

You should use these three web services:



Implement the Interface. For storing the registration information, you could hold the register as a static collection OR as a database of your own taste.

Peer Server role

At the server role, you have two functionality.

Registration / Deregistration

The registration and deregistration of files i.e. when it starts it register at the REST-service all files provided from this server
(Hint: use the File class at some 'root-catalog' and then the method getFiles() to get access to all the files to be provided or registered -- see below).
When closing the peer-server should clean up the registration by deregister all the files, whereby the register server do not have old outdated information.


The upload of a file to a peer-client, i.e. when a tcp-client connect read the first line which follow the format: 'GET filename', then send back the requested file.

Peer Client role

The get the wishes of a requested file any GUI e.g. Console, (or WPF - you have not learned how to make tcp-socket call form App's i.e. MVVM- this is rather tricky).

  1. The ask the registry service through the GET REST-service (search)
  2. From the response create a socket connection to one of the peer-servers in the List,
  3. Send the request ('GET filename') and wait for the file to be downloaded.

Code example of listing files


String path = @" << some location on the disk >> ";

if (Directory.Exists(path))
   foreach (string filename in Directory.GetFiles(path))
      // do something


Code example of Dictionary use

See github: https://github.com/rf19da2c1-1c/DemoAfDictionary


Code example of File copy

See appendix A en exercise http://pele-easj.dk/2020e-tek/exercises/socket/HttpServerChallenge.pdf