Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In part A of the assignment, your job is to write the ping client code, PingClient.java, so that the client sends 5 ping requests to

In part A of the assignment, your job is to write the ping client code, PingClient.java, so that the client sends 5 ping requests to the server, separated by approximately one second. Each ping message consists of 56 bytes of data. As in the actual ping application, these 56 data bytes can be anything, and can be the same for all ping messages. After sending each packet, the client starts a timer and waits up to one second to receive a reply. You will notice in the server code that the ping message (the 56 bytes of data in each UDP packet) is simply copied into the reply message. Once the reply is received, the client stops the timer and calculates the round trip time (rtt). If one second goes by without a reply from the server, then the client assumes that its packet or the server's reply packet has been lost in the network. For this purpose, you will need to research the API for DatagramSocket to find out how to set the timeout value on a datagram socket.

Your client should start with the following command: java PingClient where is the name of the computer the server is running on, and is the port number it is listening to. Note that you can run the client and server either on different machines or on the same machine.

When developing your code, you should run the ping server on your machine, and test your client by sending packets to localhost (or, 127.0.0.1). After you have fully debugged your code, you should see how your application communicates between two machines across the network.

Message Formatting: The ping messages in this part are to be formatted in a simple way. The client prints on the screen a one-line message corresponding to each ping request. If the client receives a response for a ping request, it prints the following line: PING where is the IP address of the server in dotted decimal format, starts at 0 and progresses to 4 for each successive ping message sent by the client, is an estimate of the round-trip time between the client and the server, calculated as the difference between the time a ping response is received and the time the request was sent (the timestamp in the response packet). The is expressed in millisecs, showing up to 3 decimal places (microsec accuracy). Note that this RTT estimate includes the AVERAGE_DELAY value that is introduced artificially, and in general may not be a good estimate of the actual RTT. If the client does not receive a response to a ping request within one second of sending it, it prints the following line: PING LOST

javaServer code:

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

/* * Server to process ping requests over UDP. */ 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 = 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) ); } }

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 Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions

Question

5. How can you listen critically to others public speeches?

Answered: 1 week ago

Question

=+How might these stem from country and regional cultures?

Answered: 1 week ago