Hello Need Help Complete Program Programming Assignment 3 Udp Pinger Lab Lab Study Simple Q43880599
Hello I need help on how to complete toprogram.
Programming Assignment 3: UDP Pinger Lab
In this lab, you will study a simple Internet ping serverwritten in the Java language, and
implement a corresponding client. The functionality provided bythese programs are similar to
the standard ping programs available in modern operatingsystems, except that they use UDP
rather than Internet Control Message Protocol (ICMP) tocommunicate with each other. (Java
does not provide a straightforward means to interact withICMP.)
The ping protocol allows a client machine to send a packet ofdata to a remote machine, and have
the remote machine return the data back to the client unchanged(an action referred to as
echoing). Among other uses, the ping protocol allows hosts todetermine round-trip times to
other machines.
You are given the complete code for the Ping server below. Yourjob is to write the Ping client.
Server Code
The following code fully implements a ping server. You need tocompile and run this code. You
should study this code carefully, as it will help you write yourPing client.
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 staticfinal 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 UDPpackets // through the port specified
on the command line. DatagramSocket socket = newDatagramSocket(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) throwsException {
// Obtain references to the packet’s array of bytes. byte[] buf= request.getData();
// Wrap the bytes in a byte array input stream, // so that youcan read the data as a stream of
bytes. ByteArrayInputStream bais = newByteArrayInputStream(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 = newInputStreamReader(bais);
// Wrap the input stream reader in a bufferred reader, // so youcan read the character data a line
at a time. // (A line is a sequence of chars terminated by anycombination of r and n.)
BufferedReader br = new BufferedReader(isr);
// The message data is contained in a single line, so read thisline. String line = br.readLine();
// Print host address and data received from it.System.out.println(
“Received from ” + request.getAddress().getHostAddress() + “: “+ new String(line) );
} }
The server sits in an infinite loop listening for incoming UDPpackets. When a packet comes in,
the server simply sends the encapsulated data back to theclient.
Packet Loss
UDP provides applications with an unreliable transport service,because messages may get lost in
the network due to router queue overflows or other reasons. Incontrast, TCP provides
applications with a reliable transport service and takes care ofany lost packets by retransmitting
them until they are successfully received. Applications usingUDP for communication must
therefore implement any reliability they need separately in theapplication level (each application
can implement a different policy, according to its specificneeds).
Because packet loss is rare or even non-existent in typicalcampus networks, the server in this lab
injects artificial loss to simulate the effects of networkpacket loss. The server has a parameter
LOSS_RATE that determines which percentage of packets should belost.
The server also has another parameter AVERAGE_DELAY that is usedto simulate transmission
delay from sending a packet across the Internet. You should setAVERAGE_DELAY to a
positive value when testing your client and server on the samemachine, or when machines are
close by on the network. You can set AVERAGE_DELAY to 0 to findout the true round trip
times of your packets.
Compiling and Running Server
To compile the server, do the following:
javac PingServer.java
To run the server, do the following:
java PingServer port
where port is the port number the server listens on. Rememberthat you have to pick a port
number greater than 1024, because only processes running withroot (administrator) privilege
can bind to ports less than 1024.
Note: if you get a class not found error when running the abovecommand, then you may need to
tell Java to look in the current directory in order to resolveclass references. In this case, the
commands will be as follows:
java -classpath . PingServer port
Your Job: The Client
You should write the client so that it sends 10 ping requests tothe server, separated by
approximately one second. Each message contains a payload ofdata that includes the keyword
PING, a sequence number, and a timestamp. After sending eachpacket, the client waits up to one
second to receive a reply. If one seconds goes by without areply from the server, then the client
assumes that its packet or the server’s reply packet has beenlost in the network.
Hint: Cut and paste PingServer, rename the code PingClient, andthen modify the code.
You should write the client so that it starts with the followingcommand:
java PingClient host port
where host is the name of the computer the server is running onand port is the port number it is
listening to. Note that you can run the client and server eitheron different machines or on the
same machine.
The client should send 10 pings to the server. Because UDP is anunreliable protocol, some of
the packets sent to the server may be lost, or some of thepackets sent from server to client may
be lost. For this reason, the client cannot wait indefinitelyfor a reply to a ping message. You
should have the client wait up to one second for a reply; if noreply is received, then the client
should assume that the packet was lost during transmissionacross the network. You will need to
research the API for DatagramSocket to find out how to set thetimeout value on a datagram
socket.
Your client will display the reply message from the server alongwith the RTT for each datagram
received or a message indicating lost packet for those notreturned within the time limit.
When developing your code, you should run the ping server onyour machine, and test your
client by sending packets to localhost (or, 127.0.0.1). Afteryou have fully debugged your code,
you should see how your application communicates across thenetwork with a ping server run by
another member of the class.
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) anda 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 eachsuccessive 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 followingexercise.
Currently the program calculates the round-trip time for eachpacket and prints them out
individually. Modify this to correspond to the way the standardping program works. You
Expert Answer
Answer to Hello I need help on how to complete to program. Programming Assignment 3: UDP Pinger Lab In this lab, you will study a …
OR