Menu

(Solved) : Hi Need Help Java Assignment Project Please Programming Assignment 3 Udp Pinger Lab Lab St Q44019200 . . .

Hi, I need help with this java assignment project, please.

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 by these programs are similar to thestandard ping programs available in modern operating systems,except that they use UDP rather than Internet Control MessageProtocol (ICMP) to communicate with each other. (Java does notprovide a straightforward means to interact with ICMP.) The pingprotocol allows a client machine to send a packet of data to aremote machine, and have the remote machine return the data back tothe client unchanged (an action referred to as echoing). Amongother uses, the ping protocol allows hosts to determine round-triptimes to other machines. You are given the complete code for thePing server below. Your job is to write the Ping client. ServerCode The following code fully implements a ping server. You need tocompile and run this code. You should study this code carefully, asit will help you write your Ping client. import java.io.*; importjava.net.*; import java.util.*; /* * Server to process pingrequests over UDP. */ public class PingServer { private staticfinal double LOSS_RATE = 0.3; private static final intAVERAGE_DELAY = 100; // milliseconds public static voidmain(String[] args) throws Exception { // Get command lineargument. if (args.length != 1) { System.out.println(“Requiredarguments: port”); return; } int port = Integer.parseInt(args[0]);// Create random number generator for use in simulating // packetloss and network delay. Random random = new Random(); // Create adatagram socket for receiving and sending UDP packets // throughthe port specified on the command line. DatagramSocket socket = newDatagramSocket(port); // Processing loop. while (true) { // Createa datagram packet to hold incomming UDP packet. DatagramPacketrequest = new DatagramPacket(new byte[1024], 1024); // Block untilthe host receives a UDP packet. socket.receive(request); // Printthe recieved data. printData(request); // Decide whether to reply,or simulate packet loss. if (random.nextDouble() < LOSS_RATE) {System.out.println(” Reply not sent.”); continue; } // Simulatenetwork 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 = newDatagramPacket(buf, buf.length, clientHost, clientPort);socket.send(reply); System.out.println(” Reply sent.”); } } /* *Print ping data to the standard output stream. */ private staticvoid printData(DatagramPacket request) throws Exception { // Obtainreferences 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); // Wrapthe byte array output stream in an input stream reader, // so youcan read the data as a stream of characters. InputStreamReader isr= new InputStreamReader(bais); // Wrap the input stream reader in abufferred reader, // so you can read the character data a line at atime. // (A line is a sequence of chars terminated by anycombination of r and n.) BufferedReader br = newBufferedReader(isr); // The message data is contained in a singleline, so read this line. String line = br.readLine(); // Print hostaddress and data received from it. System.out.println( “Receivedfrom ” + request.getAddress().getHostAddress() + “: ” + newString(line) ); } } The server sits in an infinite loop listeningfor incoming UDP packets. When a packet comes in, the server simplysends the encapsulated data back to the client. Packet Loss UDPprovides applications with an unreliable transport service, becausemessages may get lost in the network due to router queue overflowsor other reasons. In contrast, TCP provides applications with areliable transport service and takes care of any lost packets byretransmitting them until they are successfully received.Applications using UDP for communication must therefore implementany reliability they need separately in the application level (eachapplication can implement a different policy, according to itsspecific needs). Because packet loss is rare or even non-existentin typical campus networks, the server in this lab injectsartificial loss to simulate the effects of network packet loss. Theserver has a parameter LOSS_RATE that determines which percentageof packets should be lost. The server also has another parameterAVERAGE_DELAY that is used to simulate transmission delay fromsending a packet across the Internet. You should set AVERAGE_DELAYto a positive value when testing your client and server on the samemachine, or when machines are close by on the network. You can setAVERAGE_DELAY to 0 to find out the true round trip times of yourpackets. Compiling and Running Server To compile the server, do thefollowing: javac PingServer.java To run the server, do thefollowing: java PingServer port where port is the port number theserver listens on. Remember that you have to pick a port numbergreater than 1024, because only processes running with root(administrator) privilege can bind to ports less than 1024. Note:if you get a class not found error when running the above command,then you may need to tell Java to look in the current directory inorder to resolve class references. In this case, the commands willbe as follows: java -classpath . PingServer port Your Job: TheClient You should write the client so that it sends 10 pingrequests to the server, separated by approximately one second. Eachmessage contains a payload of data that includes the keyword PING,a sequence number, and a timestamp. After sending each packet, theclient waits up to one second to receive a reply. If one secondsgoes by without a reply from the server, then the client assumesthat its packet or the server’s reply packet has been lost in thenetwork. Hint: Cut and paste PingServer, rename the codePingClient, and then modify the code. You should write the clientso that it starts with the following command: java PingClient hostport where host is the name of the computer the server is runningon and port is the port number it is listening to. Note that youcan run the client and server either on different machines or onthe same machine. The client should send 10 pings to the server.Because UDP is an unreliable protocol, some of the packets sent tothe server may be lost, or some of the packets sent from server toclient may be lost. For this reason, the client cannot waitindefinitely for a reply to a ping message. You should have theclient wait up to one second for a reply; if no reply is received,then the client should assume that the packet was lost duringtransmission across the network. You will need to research the APIfor DatagramSocket to find out how to set the timeout value on adatagram socket. Your client will display the reply message fromthe server along with the RTT for each datagram received or amessage indicating lost packet for those not returned within thetime limit. When developing your code, you should run the pingserver on your machine, and test your client by sending packets tolocalhost (or, 127.0.0.1). After you have fully debugged your code,you should see how your application communicates across the networkwith a ping server run by another member of the class. MessageFormat The ping messages in this lab are formatted in a simple way.Each message contains a sequence of characters terminated by acarriage return character (r) and a line feed character (n). Themessage contains the following string: PING sequence_number timeCRLF where sequence_number starts at 0 and progresses to 9 for eachsuccessive ping message sent by the client, time is the time whenthe client sent the message, and CRLF represent the carriage returnand line feed characters that terminate the line. When you arefinished writing the client, complete the following exercise.Currently the program calculates the round-trip time for eachpacket and prints them out individually. Modify this to correspondto the way the standard ping program works. You will need to reportthe minimum, maximum, and average RTTs.

Expert Answer


Answer to Hi, I need help with this java assignment project, please. Programming Assignment 3: UDP Pinger Lab In this lab, you wil…

OR