Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, can you do the inline comment the pseudo code for this java program please because I need the learn the code. /Clientping.java import java.io.*;

Hi, can you do the inline comment the pseudo code for this java program please because I need the learn the code.

/Clientping.java

import java.io.*; import java.net.*; import java.util.*;

public class ClientPing { private static final double LOSS_RATE = 0.3; private static final int AVERAGE_DELAY = 100; // milliseconds static DatagramPacket request1; static DatagramPacket response1; static DatagramSocket socket1; static boolean received = false; public static int num = 0; /* PingClient() { Thread thisThread = new Thread( this ); thisThread.start(); } */ public static void main(String[] args) throws Exception { // Get command line argument. if (args.length < 2) { System.out.println("Required arguments: port, address"); return; } int port = Integer.parseInt(args[0]);

// Create random number generator for use in simulating // packet loss and network delay. Random random = new Random();

// Create a datagram socket for receiving and sending UDP packets // through the port specified on the command line. socket = new DatagramSocket(port); /* DatagramConnection sender = (DatagramConnection)Connector.open("datagram://target:32767"); */

// Processing loop. received = false; while (!received) { byte kilo[] = new byte[1024]; char[] transfer; String num2 = "" + num; transfer = num2.toCharArray(); for(int i = 0; i < transfer.length; i++) { kilo[i] = (byte)transfer[i]; } // Create a datagram packet to hold outgoing UDP packet. InetAddress address = InetAddress.getByName(args[1]); request1 = new DatagramPacket(kilo, kilo.length, address /* InetAddress ipAddress */ , /* int port */ 1024); // port 1024 // InetAddress.getLocalHost() response2 = new DatagramPacket(kilo, kilo.length);

socket1.send(request1);

System.out.println("Starting thread"); PingThread ping = new PingThread(request1, response1, socket1, received, num);

// ping.start();

// System.out.println("hello"); socket1.receive(response1); // REVEIEVE NOTHING BACK // Thread.sleep(200); ping.myStop(); System.out.println("hello");

// System.out.println("Hello; 3"); /* InetAddress serverHost = request1.getAddress(); int serverPort = request1.getPort(); byte[] buf = request1.getData(); DatagramPacket reply = new DatagramPacket(buf, buf.length, serverHost, serverPort); socket1.send(reply); */ // end insertion

printData(response1); received = true; /* // Decide whether to reply ELSE simulate packet loss. if (random.nextDouble() < LOSS_RATE) { System.out.println("Reply not sent."); continue; }

// Simulate network delay. Thread.sleep((int) (random.nextDouble() * 2 * AVERAGE_DELAY));

// reply SEND. InetAddress clientHost = request1.getAddress(); int clientPort = request1.getPort(); byte[] buf = request.getData(); DatagramPacket reply = new DatagramPacket(buf, buf.length, clientHost, clientPort); // a length here socket.receive(reply);

System.out.println(" recieved reply."); */ //received = false; //num++; //if(num == 11) //return; //System.out.println(num); } }

/* public void run() { System.out.println("Hello"); try { Thread.sleep(200); } catch(InterruptedException e) { System.out.println("Bug detected: " + e); } if(!received) { try { socket1.send(request1); Thread thisThread = new Thread( this ); thisThread.start(); socket1.receive(response1); } catch(IOException e) { System.out.println("the Bug detected: " + e); } } } */

/* * Print ping data to the standard output stream. */ public static void printData(DatagramPacket request) throws Exception { // Obtain references to the packet's array of bytes. byte[] buf = request1.getData();

// Wrap/cover the bytes in a byte array input stream, // since, you could read the data as a stream of bytes. ByteArrayInputStream bais = new ByteArrayInputStream(buf);

// Wrap/cover the byte array output stream in an input stream reader, // since you could read the data as a stream of characters. InputStreamReader isr = new InputStreamReader(bais);

// Wrap/cover the input stream reader in a bufferred reader, // since you can read the character data a line at a time. // BufferedReader br = new BufferedReader(isr);

// The msg data is contain in the single line, so you can read this line. String line = br.readLine();

// Print host address along with data received from it. System.out.println( "Received from " + request1.getAddress().getHostAddress() + ": " + new String(line) ); } }

// ThreadPing.java

import java.io.*; import java.net.*; import java.util.*;

/** * Write a description of class ThreadPing here.

*/ public class ThreadPing implements Runnable { static DatagramPacket request1; static DatagramPacket response1; static DatagramSocket socket1; static boolean received = false; static int num1; public static boolean stopFlag = false; public void myStop() { System.out.println("used stop flag"); received = true; stopFlag = true; } PingThread(DatagramPacket request1, DatagramPacket response1, DatagramSocket socket1, boolean received, int num1) { stopFlag = false; this.request1 = request1; this.response1 = response1; this.socket1 = socket1; this.received = received; this.num1 = num1; Thread thisThread = new Thread( this ); thisThread.start(); } public void run() { // ClientPing check = new ClientPing(); // System.out.println("Hello"); while(!received) {

try { Thread.sleep(200); // problem is that this will wait for too long if(stopFlag) { System.out.println("returning"); break; } System.out.println("num1: " + num1); } catch(InterruptedException e) { System.out.println("the Bug detected: " + e); } //if(!received) //{ try { socket1.send(request1); // Thread thisThread = new Thread( this ); // this could be the problem // thisThread.start(); // socket1.receive(response1); num1++; } catch(IOException e) { System.out.println("THE Bug detected: " + e); } //} } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Second International Conference Acids Hue City Vietnam March 2010 Proceedings Part 1 Lnai 5990

Authors: Manh Thanh Le ,Jerzy Swiatek ,Ngoc Thanh Nguyen

2010th Edition

3642121446, 978-3642121449

More Books

Students also viewed these Databases questions

Question

Have I incorporated my research into my outline effectively?

Answered: 1 week ago