Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'd like to display RTT or loss per reply, and calculate the min, max, and avg RTT in this program (Java): import java.io.*; import java.net.*;

I'd like to display RTT or loss per reply, and calculate the min, max, and avg RTT in this program (Java):

import java.io.*; import java.net.*; import java.util.*; // Java PingClient host port // type localhost 1024 after compiling this program, also ensure PingServer.java is running // The client will process Ping requests over UDP public class PingClient { // Setting a max timeout// private static final int maxTimeout = 1000;

public static void main (String[] args) throws Exception { // if statement requiring user to provide Server Port number if (args.length != 2) { System.out.println("Server port required."); return; } // Port number to be used int port = Integer.parseInt(args[1]);

// PingServer needs to run in order to work InetAddress server; server = InetAddress.getByName(args[0]);

// Creating DatagramSocket for UDP @SuppressWarnings("resource") DatagramSocket socket = new DatagramSocket(port);

// Sequencing int sequenceNum = 0;

// loop until 10 UDP pings while (sequenceNum < 10) { // Timestamp in milliseconds when sent. Date currentTime = new Date(); long send = currentTime.getTime(); // String to send as byte array String str = "Ping " + sequenceNum + " " + send + " "; byte[] seq = new byte[1024]; seq = str.getBytes();

// Create a Datagram Packet to send as UDP packet. DatagramPacket ping = new DatagramPacket (seq, seq.length, server, port);

// Send the DatagramPacket ping to the server. socket.send(ping); // Try/catch failures try { // Timeout set socket.setSoTimeout(maxTimeout); // Datagram socket for transmission of UDP Packets DatagramPacket response = new DatagramPacket (new byte[1024], 1024); // If response received continue, if not // will catch and continue socket.receive(response);

// this will timestamp the packet retrieval Date now = new Date(); long receive = currentTime.getTime(); // This will print packet data and delays printData(response, receive - send); } catch (IOException e) { // Print which packet has timed out. System.out.println("Packet " + sequenceNum + "has timed out."); } // Continues onto the next packet in sequence sequenceNum++; } }

/* * This will print ping data to the standard output stream */ private static void printData(DatagramPacket request, long delayTime) throws Exception { // This will obtain references to the packets array of bytes byte[] seq = request.getData(); // Wrap bytes in a byte Array input stream, // so the data is readable as stream of bytes. ByteArrayInputStream ba = new ByteArrayInputStream(seq);

// Wrap byte Array input stream in an input // stream reader, so the data as a stream of // characters InputStreamReader is = new InputStreamReader(ba);

// Buffered reader so character data can be read a line at a time BufferedReader br = new BufferedReader(is);

// This is so everything is contained in a single line String line = br.readLine(); // This line will print host info and data received System.out.println("Received from " + request.getAddress().getHostAddress() + ": " + new String(line) + " Delay: " + delayTime); } }

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

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions