Assignment 2: A Peer Node

1. Objectives

The majority of socket programs, including GetImage/imageDB of assignment 1, follows the client-server paradigm, where a server waits on a well-known port for clients’ connections. In this assignment, you’ll explore peer-to-peer programming. If you didn’t manage to get your code to work in assignment1, no problem you still have the chance to practice socket-programming in this assignment. It is also expected, but not necessarily, to re-use some of the methods you built in assignment1. A peer is basically both a server and a client. It accepts connections from other peers and also connects to one or more peers.

2. Introduction

The peer program takes three optional arguments on the command line:

> node peer [-p <peerIP>:<port>]

The -p option tells the peer program which peer to connect to initially. If this option is not provided, the peer starts as a server listening on a random, ephemeral port.

To bootstrap the peer-to-peer (p2p) network, we first start a peer by itself. Every time a peer runs, it prints its IP address and port number it is listening on. When a peer is run with the peerIPs:port of another peer N as its command line argument, the new peer starts as a server listening on a random, ephemeral port, and at the same time, it tries to join the peer N by creating a socket and connecting to the peer N.

A peer that receives a join request will accept the peer if and only if its peer table is not full. Whether a join request is accepted or not, the peer sends back to the requesting peer the IPaddress:port of a peer that is saved in its peer table (if the table is not empty). This will help the newly joined peer find more peers to join.

Abdelkader Ouda page 1 Winter 2021

In this assignment, a custom Peer-to-Peer Transport Protocol (cPTP) for peer communication will be built and used. The subsequent sections describe the structure and mechanism of this protocol.

3. Task 1: Server Side

Your first task is to implement the server side of the peer program. If peer is run without any option on the command line, the peer initializes a TCP socket and obtain an ephemeral port number from the kernel which it prints out to the console. The peer will also start a timer to tick every 10 milliseconds, that is initialized by a random value from 1 to 999 and keep incremented by 1 every tick along the lifetime of the peer execution.

If a new peer is trying to connect to this peer and this peer’s peering table is not full, handleClientJoining(sock) (given in assignment1) should accept the connection and then send back a message packet (see Figure 1) with field set to 1, (1 means Welcome). The new peer s information is then stored in the peer table. On the other hand, if the peer table is full, handleClientJoining(sock) is invoked again to receive the request but it sends back a redirect message packet

-direct). Note that, the peer table size of each peer is determined All times it fills in the fields of the message, V must be set to 7. holds the number of peers

information attached to the cPTP message packet.
The figure below shows the cPTP message packet structure.



本网站支持淘宝 支付宝 微信支付  paypal等等交易。如果不放心可以用淘宝交易!

E-mail:  微信:itcsdx