Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I need to write a java program that connects to 2 successor peers and simult

ID: 3532927 • Letter: H

Question

Hi,


I need to write a java program that connects to 2 successor peers and simultaneously waits for 2 predecessor peers to connect to it.


This program is to implement a P2P protocol.


Steps involved:

Initialization

Ping Mechanism


Input and Initialization Specifications:

Takes in 3 integer arguments. (e.g. java cdht 1 42 58)

First argument ("1") is the identity of the peer to be initialized

Second argument is successor1,

Third argument is sucessor2 ("42" and "58") are the two successive peers.


Notes:

Max 10 peers.

Identities of peers in the range of [0, 255].

Use local host as IP Adress ("127.0.0.1")

Use UDP protocol at port 50,000 + x for x = peer number (First argument)


Ping Mechanism

A peer regularly pings its successive peers (42 and 58) via their port numbers (50,042 and 50,058) and receives back a response.

When receiving either a request or response please output a string:

"A ping response/request was received from peer X" where X is the peer's identity (1, 42, 58)



The starting code can be found at this download link:


https://dl.dropbox.com/u/40094614/CDHT/CDHT.java



or you can copy from below:


--------------------------------------------------------------------------

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.ServerSocket;

import java.net.Socket;



public class CDHT {


static int portSelf;

static int portSuccessor1;

static int portSuccessor2;

static int portStart = 50000;


public static void main(String[] args) {


// Step 1 - Taking in Arguments

portSelf = portStart + Integer.parseInt(args[0]);

portSuccessor1 = portStart + Integer.parseInt(args[1]);

portSuccessor2 = portStart + Integer.parseInt(args[2]);


// Step 2 - Creating the ping mechanism

// Create server socket threads that waits allows 2 simultaneous connections.

// When connected, it responds with a HI when a HELO message is received.



// Create client socket threads that tries to connect to

// and sends a HELO message to both portSuccessor1 and portSuccessor2 every 5 seconds.




}


}


Explanation / Answer

Check this code :


import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.ServerSocket;

import java.net.Socket;


class SS1 implements Runnable

{

private int port;

public SS1(int p)

{

this.port=p;

Thread t=new Thread(this);

t.start();

}

@Override

public void run()

{

try {

ServerSocket s2=new ServerSocket(port);

Socket sock=s2.accept();

DataInputStream d=new DataInputStream(sock.getInputStream());

DataOutputStream dos=new DataOutputStream(sock.getOutputStream());

while(d.readUTF().equals("HELO"))

{

System.out.println("HELO Msg Received from "+sock);

dos.writeUTF("HI");

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


class CS1 implements Runnable

{

private int port;

public CS1(int p)

{

this.port=p;

Thread t=new Thread(this);

t.start();

}

@Override

public void run()

{

try {

Socket s2=null;

while(s2==null)

{

try{

s2=new Socket("127.0.0.1",port);

}

catch(Exception e){}

}

DataInputStream d=new DataInputStream(s2.getInputStream());

DataOutputStream dos=new DataOutputStream(s2.getOutputStream());

while(true)

{

dos.writeUTF("HELO");

System.out.println("Msg Received in Client 1 "+d.readUTF());

Thread.sleep(5000);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


class SS2 implements Runnable

{

private int port;

public SS2(int p)

{

this.port=p;

Thread t=new Thread(this);

t.start();

}

@Override

public void run()

{

try {

ServerSocket s2=new ServerSocket(port);

Socket sock=s2.accept();

DataInputStream d=new DataInputStream(sock.getInputStream());

DataOutputStream dos=new DataOutputStream(sock.getOutputStream());

while(d.readUTF().equals("HELO"))

{

System.out.println("HELO Msg Received from "+sock);

dos.writeUTF("HI");

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


class CS2 implements Runnable

{

private int port;

public CS2(int p)

{

this.port=p;

Thread t=new Thread(this);

t.start();

}

@Override

public void run()

{

try {

Socket s2=null;

while(s2==null)

{

try{

s2=new Socket("127.0.0.1",port);

}

catch(Exception e){}

}

DataInputStream d=new DataInputStream(s2.getInputStream());

DataOutputStream dos=new DataOutputStream(s2.getOutputStream());

while(true)

{

dos.writeUTF("HELO");

System.out.println("Msg Received in Client 2 "+d.readUTF());

Thread.sleep(5000);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


public class CDHT {


static int portSelf;

static int portSuccessor1;

static int portSuccessor2;

static int portStart = 50000;


public static void main(String[] args) throws IOException {


// Step 1 - Taking in Arguments

portSelf = portStart + Integer.parseInt(args[0]);

portSuccessor1 = portStart + Integer.parseInt(args[1]);

portSuccessor2 = portStart + Integer.parseInt(args[2]);


System.out.println(portSuccessor1+" "+portSuccessor2);

// Step 2 - Creating the ping mechanism

// Create server socket threads that waits allows 2 simultaneous connections.

// When connected, it responds with a HI when a HELO message is received.

new SS1(portSuccessor1);

new SS2(portSuccessor2);

// Create client socket threads that tries to connect to

// and sends a HELO message to both portSuccessor1 and portSuccessor2 every 5 seconds.


new CS1(portSuccessor1);

new CS2(portSuccessor2);


}


}


Happy to Help :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote