subject

In this assignment, you will study a simple Internet ping server written in the Java language, and implement a corresponding client. The functionality provided by these programs is similar to the standard ping programs available in modern operating systems, except that they use UDP rather than Internet Control Message Protocol (ICMP) to communicate with each other. (Java does not provide a straightforward means to interact with ICMP.) The ping protocol allows a client machine to send a packet of data 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 to determine round-trip times to other machines.
You are given the complete code for the Ping server below. Your job is to write the Ping client.
Server Code
The following code fully implements a ping server. You need to compile and run this code. You should study this code carefully, as it will help you write your Ping 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 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 \r and \n.)
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) );
}
}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 02:00
Consider how gaming consoles initially relied on joysticks and joypads and then made the switch to modern gaming controls, which include analog sticks, buttons and switches, touch controls, accelerometers, motion controls, etc. name at least two kinds of gaming experiences that are possible with these new control devices but were not possible on original joysticks. explain how new technologies made this newer game style possible.
Answers: 1
question
Computers and Technology, 23.06.2019 09:30
:you areto design the controller for alight that functions both as an ordinary light and also as a motion activated light and alarm. a.if the manual switch s is on, then the light l is on. b.besides the manual switch, there is a motion detector, m1, which activatesthis light.c.if motion is detected but the light is on anyway because s is on, only then a secondoutput a, an alarm, is turned on. d.the disable switch, d, disables the motion activated light and alarmbut leaves manual control operation of the light using switch s.(i)read the problem statement and clearly identify the inputs and outputs for the circuit you are designing. (ii)create the truth table for this system; include the light, alarm, switch, disable, and the motion sensor.(iii)draw a schematic of this system.
Answers: 1
question
Computers and Technology, 23.06.2019 16:30
20 points archie wants to use a reflector as he photographs a newlywed couple. what would he consider in his choice? a. shadow and sunny b. homemade and professional c. lamps and boards d. incident and reflected e. neutral density and enhancement
Answers: 3
question
Computers and Technology, 23.06.2019 19:50
Which feature is selected to practice and save the timing of a presentation
Answers: 1
You know the right answer?
In this assignment, you will study a simple Internet ping server written in the Java language, and i...
Questions
question
Chemistry, 28.04.2021 18:50
question
Mathematics, 28.04.2021 18:50
question
Mathematics, 28.04.2021 18:50
question
Social Studies, 28.04.2021 18:50
Questions on the website: 13722363