In lab Assignment 2 - Modeling a Peer-to-Peer (p2p) Network



This week you will further enhance your socket programming skills. What's new in this week:



For the basic assignment you will write 3 programs, namely:
  1. Client
  2. Forwarder
  3. Server

In more detail, during this lab you are going to model and implement some simple functionalities of a peer-to-peer (p2p) network. The main difference of p2p compare to the classical client-server paradigm is that roles (that is, who is the client and who is the server) are interchanged between peers. For example, in the general case, when you download a file you act as a client but at the same time you can act as a server by sharing your files to others. Moreover, in contrast to the client-server model where the client always knows the IP address (i.e, by using DNS service in order to translate www.google.com to an IP address) and the port number (i.e, if the applications is Web then the server is listening to Port 80 which is the well-known port dedicated for the particular application) of the server, however this is not the case in a p2p network. In order to address this problem, peers cooperate and forward requests and information about the availability of files to each other.


To complete the Lab you have to implement 5 peers:
When implementing the 5 peers (5 peers but only 3 programs) you can make the following assumptions
To complete the Lab you should implement the following scenario:
  1. The forwarder is loaded (remember to use the ifconfig from the command line in order to get the IP address of the system hosting your forwarder process).
    • The forwarder initially listens at a particular port for a connection issued from the client.
    • The forwarder knows a priory the {IP Address, Port Number} of each of the three servers.
    • As well as the name of the file shared by each server peer. To make your life easier, use a single character for each file (for example, ‘a’, ‘b’ and ‘c’).

  2. The three server peers are loaded. We strongly recommend that you execute each server on the same system (hence, under the same IP) and use ports 3535, 3536, 3537 for the first, the second and the third server peer respectively.
    • Remember to give to each server a unique name (give this value from the command line).
    • Each server initially listens at a particular port (3535 or 3536 or 3537) for a connection issued from the forwarder.

  3. Finally the client is loaded. The client knows the {IP Address, Port Number} of the forwarder peer.

  4. The client issues a single request for a single file (you can give the name of the requested file from the command like during the execution of the client process) towards the forwarder. Remember to include the {IP Address, Port Number} that the client will be listening (the client will have to listen for a reply from a server).

  5. The forwarder receives the request and replies to the client with a string:
    • "File found" if the requested file is the same as one of the files shared by the server peers.
    • Or with "File not found" if the requested file is not available in the p2p network.

  6. If the file is found. The forwarder gets the {IP Address, Port Number} of the server peer that has the particular file and issues a connection towards him in order to inform him that a peer (client) is interested for its file. In the same single message exchanged by forwarder and server, the forwarder includes the {IP Address, Port Number} of the client (these information were given to the forwarder, by the client , during the client's request for the particular file).

  7. The server uses the {IP Address, Port Number} of the client in order to connect with him (Hint: the client, after issuing the request to the forwarder, must create a listener socket). When the connection is established the server sends its unique name (string) to the client.

  8. The client prints the unique name he received from the server to the screen. The end!

What you must show to the TA:

The aforementioned scenario is graphically illustrated in the following figure:


Appendix


Byte Ordering

Port numbers and IP Addresses (both discussed next) are represented by multi-byte data types which are placed in packets for the purpose of routing and multiplexing. Port numbers are two bytes (16 bits) and IP4 addresses are 4 bytes (32 bits), and a problem arises when transferring multi-byte data types between different architectures. Say Host A uses a “big-endian” architecture and sends a packet across the network to Host B which uses a “little-endian” architecture. If Host B looks at the address to see if the packet is for him/her (choose a gender!), it will interpret the bytes in the opposite order and will wrongly conclude that it is not his/her packet. The Internet uses big-endian and we call it the network-byte-order, and it is really not important to know which method it uses since we have the following functions to convert host-byte-ordered values into network-byte-ordered values and vice versa:

To convert port numbers (16 bits):

Host -> Network
unsigned short htons( hostportnumber ) /* Note: hostportnumber can be a value returned from atoi() */

Network -> Host
unsigned short ntohs( netportnumber )

To convert IP4 Addresses (32 bits):

Host -> Network
unsigned int htonl( hostIPv4number )

Network -> Host
unsigned int ntohl( netIPv4number )

Note: To convert from string (e.g, "129.12.33.244" ) to an Internet Address use the fucntion inet_addr(IP_address_as_string);


When you are writting your programms keep in mind the 3 holy rules of programming:
  • KISS (Keep It Simple, Stupid)
  • FMIRTMIF (First Make It Right Then Make It Fast)
  • RTFM (Read The F***ing Manual)