Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help to modify the java program code below, here the instruction: Can you change while loop to for loop? Explore how to timeout

I need help to modify the java program code below, here the instruction:

Can you change while loop to for loop?

Explore how to timeout the socket

The message should be Ping with a sequence number and time stamp.

The solution must include a loop, client socket, datagram, message, and display RTT (round trip time) or loss per reply, and calculate the min, max, and avg RTT. Clear and efficient Java coding is expected.

The code to modify is below:

PingClient code:

import java.io.*;

import java.net.*;

import java.util.*;

public class PingServer

{

private static final double LOSS_RATE = 0.3;

private static final int AVERAGE_DELAY = 100; // milliseconds

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

{

// Get command line argument.

if (args.length != 1)

{

System.out.println("Required arguments: port");

return;

}

int port = 8888; //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.

DatagramSocket socket = new DatagramSocket(port);

// Processing loop.

while (true)

{

// Create a datagram packet to hold incomming UDP packet.

DatagramPacket request = new

DatagramPacket(new byte[1024], 1024);

// Block until the host receives a UDP packet.

socket.receive(request);

// Print the recieved data.

printData(request);

// Decide whether to reply, or 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));

// Send reply.

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.");

}

}

/* * Print ping data to the standard output stream. */

private static void printData(DatagramPacket request) throws Exception

{

// Obtain references to the packet's array of bytes.

byte[] buf = request.getData();

// Wrap the bytes in a byte array input stream, // so that you can read the data as a stream of bytes.

ByteArrayInputStream bais = new ByteArrayInputStream(buf);

// Wrap the byte array output stream in an input stream reader, // so you can read the data as a stream of characters.

InputStreamReader isr = new InputStreamReader(bais);

// Wrap the input stream reader in a bufferred reader, // so you can read the character data a line at a time.

// (A line is a sequence of chars terminated by any combination of and .)

BufferedReader br = new BufferedReader(isr);

// The message data is contained in a single line, so read this line.

String line = br.readLine();

// Print host address and data received from it.

System.out.println("Received from " + request.getAddress().getHostAddress() + ": " + new String(line));

}

}

PingClient Code:

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) {

//Comment line argument

if (args.length < 2) {

System.out.println("Required argements: Port Number");

return;

}

// Pass as an argument Which should be server

// hostnames so we are running both server and client on same machine,

// the host name will be 127.0.0.1

String hostname = "127.0.0.1";

//Assign Port number

int port = 8888;

try {

//InetAddress is used to represent the ip address

InetAddress address = InetAddress.getByName(hostname);

//create socket as opening point of connection

DatagramSocket socket = new DatagramSocket();

//infinite loop

while (true) {

// Message to the server

String data = "Welcome to Data Communictions Class!";

//Sent the packet with length,address and port

DatagramPacket request = new DatagramPacket(data.getBytes(), data.length(), address, port);

socket.send(request);

//Create byte array of lenght 1024

byte[] buffer = new byte[1024];

//If the package receive.It will wait till socket not receives the reply from server

DatagramPacket response = new DatagramPacket(buffer, buffer.length);

socket.receive(response);

// This code will be execute only if relpy from server is received

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

System.out.println(recieveReply);

System.out.println();

//Wait for 10000 milliseconds

Thread.sleep(10000);

}

// if socket is not ready

} catch (SocketTimeoutException ex) {

System.out.println("Error Timeout: " + ex.getMessage());

ex.printStackTrace();

// Any Input or Output Exception

} catch (IOException ex) {

System.out.println("Client error: " + ex.getMessage());

ex.printStackTrace();

// Any other interrupt

} 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_2

Step: 3

blur-text-image_3

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

Students also viewed these Databases questions

Question

=+X. Is it feasible to perform Function X? For instance,

Answered: 1 week ago