Question
I need help to modify the java code assignment below, please: Project assignment Use a for-loop -Use simple System time -Use socket timeout method -Use
I need help to modify the java code assignment below, please:
Project assignment
Use a for-loop
-Use simple System time
-Use socket timeout method
-Use simple printout solution
-Extract the timestamp from the received message and use this in calculating RTT
-Exclude dropped packets from the avg calculation
- The solution must include a loop, client socket, datagram, message, and display RTT or loss per reply, and calculate the min, max, and avg RTT. Clear and efficient Java coding is expected.
RT-click and copy the PingServer.java code below to a folder on your system. Compile it with javac. Execute it with java command. When running the server sits quietly waiting for your client to communicate with it on the port/socket it constructs
You should write the client so that it sends 10 ping requests to the server, separated by
approximately one second. Each message contains a payload of data that includes the keyword
PIN
G, a sequence number, and a timestamp. After sending each packet, the client waits up to one
second to receive a reply. If one seconds goes by without a reply from the server, then the client
assumes that its packet or the server's reply packet has been lo
st in the network.
The client should send 10 pings to the server.
Because UDP is an unreliable protocol, some of
the packets sent to the server may be lost, or some of the packets sent from server to client may
be lost. For this reason, the client
cannot
wait indefinitely for a reply to a ping message. You
should have the client wait up to one second for a reply; if no reply is received, then the client
should assume that the packet was lost during transmission across the network. You will need to
research the API for DatagramSocket to find out how to set the timeout value on a datagram
socket.
Your client will display the reply message from the server along with the RTT for each datagram
received or a message indicating lost packet for those not returned within the time limit.
When developing your code, you should run t
he ping server on your machine, and test your
Message Format
The ping messages in this lab are formatted in a simple way. Each message contains a sequence
of characters terminated by a carriage return character (r) and a line feed character (n). The
message contains the following string:
PING sequence_number time CRLF
where sequence_number starts at 0 and progresses to 9 for each successive ping message sent by
the client, time is the time when the client sent the message, and CRLF represent the carriage
return and line feed characters
that terminate the line.
When you are finished writing the client, complete the following exercise.
Currently the program calculates the round-trip time for each packet and prints them out
individually. Modify this to correspond to the way the standard ping program works. You
will need to report the minimum, maximum, and average RTTs.
PingServer 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started