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. PingServer.java ============= import

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

PingServer.java

=============

import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Random;

public class PingServer { private static final double LOSS_RATE = 0.3; private static final int AVERAGE_DELAY = 100;

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

if (args.length != 1) { System.out.println("Required arguments: port"); return; }

int port = Integer.parseInt(args[0]); // Port number.On which port you want to run your server for example 9999.

Random random = new Random(); DatagramSocket socket = new DatagramSocket(port); while (true) { DatagramPacket request = new DatagramPacket(new byte[1024], 1024);

socket.receive(request); printData(request); if (random.nextDouble() < LOSS_RATE) { System.out.println(" Reply not sent."); continue; } Thread.sleep((int) (random.nextDouble() * 2 * AVERAGE_DELAY)); InetAddress clientHost = request.getAddress(); int clientPort = request.getPort(); byte[] buf = request.getData(); DatagramPacket reply = new DatagramPacket(buf, buf.length, clientHost, clientPort); socket.send(reply); System.out.println(" Reply sent."); } }

private static void printData(DatagramPacket request) throws Exception { byte[] buf = request.getData(); ByteArrayInputStream bais = new ByteArrayInputStream(buf); InputStreamReader isr = new InputStreamReader(bais); BufferedReader br = new BufferedReader(isr); String line = br.readLine(); System.out.println("Received from " + request.getAddress().getHostAddress() + ": " + new String(line)); } }

PingClient.java

============

import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketTimeoutException;

public class PingClient { public static void main(String[] args) {

if (args.length < 2) { System.out.println("Syntax: QuoteClient "); return; }

String hostname = args[0]; // Need to pass as an argument at position zero. Which should be server // hostname. In our case we are running both server and client on same machine, // so host name will be 127.0.0.1

int port = Integer.parseInt(args[1]); // Port number. On which port server application is running for example // 9999.

try { InetAddress address = InetAddress.getByName(hostname); DatagramSocket socket = new DatagramSocket();

while (true) { String welcome = "Welcome to JAVA!"; DatagramPacket request = new DatagramPacket(welcome.getBytes(), welcome.length(), address, port); socket.send(request);

byte[] buffer = new byte[512]; DatagramPacket response = new DatagramPacket(buffer, buffer.length); socket.receive(response);

String quote = new String(buffer, 0, response.getLength());

System.out.println(quote); System.out.println();

Thread.sleep(10000); }

} catch (SocketTimeoutException ex) { System.out.println("Timeout error: " + ex.getMessage()); ex.printStackTrace(); } catch (IOException ex) { System.out.println("Client error: " + ex.getMessage()); ex.printStackTrace(); } catch (InterruptedException ex) { ex.printStackTrace(); } } }

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

Students also viewed these Databases questions

Question

In a hypothesis test with hypotheses Ho :p Z 0.31 and H1 :p

Answered: 1 week ago

Question

The paleolithic age human life, short write up ?

Answered: 1 week ago