(This is Java) I need help with part A (please explain every line) . PART A. Wri
ID: 3790059 • Letter: #
Question
(This is Java) I need help with part A (please explain every line)
.
PART A. Write a client program and a server program to implement the following protocol on top of UDP. • Client Program: 1. Display a message to ask the User to input the DNS or IP of the machine on which the Server Program runs. 2. Display the following table on the standard output: Item ID Item Description 00001 New Inspiron 15 00002 New Inspiron 17 00003 New Inspiron 15R 00004 New Inspiron 15z Ultrabook 00005 XPS 14 Ultrabook 00006 New XPS 12 UltrabookXPS 3. Display a message on the standard output to ask the User to input an Item ID, and validate the user input. If the input is not a valid Item ID, ask the User to re-type it. 4. Once getting a valid item ID from the User, send a request including this Item ID (e.g., 00005 or “00005”) to the Server program to ask for a quote, and record the local time right before sending such request. 5. Receive and interpret the response from the Server program, get the local time right after such response is received, and display the following information on the standard output, (e.g., if 00005 were provided by the User earlier on) Item ID Item Description Unit Price Inventory RTT of Query 00005 XPS 14 Ultrabook $999.99 261 … ms where “RTT of Query” is the difference between the time in Steps 5 and 4 in millisecond. 6. Display a message on the standard output to ask the User whether to continue. If yes, repeat steps 2 through 4. Otherwise, close the socket and terminate the Client program.
PART B Server Program: 1. Maintain the following information using an appropriate data structure of your choice (i.e., an array of a Class you defined). You do not have to place it in a file although you certainly can if you like. Item ID Item Description Unit Price Inventory 00001 New Inspiron 15 $379.99 157 00002 New Inspiron 17 $449.99 128 00003 New Inspiron 15R $549.99 202 00004 New Inspiron 15z Ultrabook $749.99 315 00005 XPS 14 Ultrabook $999.99 261 00006 New XPS 12 UltrabookXPS $1199.99 178 2. Wait for receiving a packet from a Client. 3. Once a packet is received from a Client, retrieve the information relevant to the requested Item ID from the data structure you used in Step 1 and send back such information to the Client. 4. Repeat Step 3 infinitely until an exception is caught. 5. Close the datagram socket.
Explanation / Answer
[8:26:37 AM] Learn and Earn: UDPClient.java
import java.io.*;
import java.net.*;
import java.sql.Timestamp;
/**
* UDPClient requests item inventory from a remote server.
*/
public class UDPClient {
// UDP socket for client/server communication
private DatagramSocket udpSocket;
// Input from user command line
private BufferedReader systemInput;
// Messages befing sent from server and user
private String msgFromServer, msgFromUser;
// Address of the server to connect to
private InetAddress address;
// Message buffer for sending messages to server
private byte[] bufferQuery;
// udpPacketRequest is packet being sent to server
// udpPacketResponse is packet being received from server
private DatagramPacket udpPacketRequest, udpPacketResponse;
// The message buffer for receiving messages from server
private byte[] bufferReply;
// Times used to calculate elapsed time
private Timestamp requestTime, responseTime;
/**
* main method creates a new UDPClient.
*/
public static void main(String[] args) {
new UDPClient();
}
/**
* Instantiates a new UDP client and prompts for a server address.
*/
public UDPClient() {
// Create an UDPSocket for client/server communication
try {udpSocket = new DatagramSocket();}
catch (SocketException e) {
System.out.println("Unable to create DatagramSocket.");
e.printStackTrace();
}
systemInput = new BufferedReader(new InputStreamReader(System.in));
msgFromServer = null;
msgFromUser = null;
// Ask user for Internet address of the server
System.out.println(" Please input the DNS or IP of the machine on which the Server Program runs");
try {address = InetAddress.getByName(systemInput.readLine());}
catch (UnknownHostException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
// Send packet containting 'initialize' to let server know a new client is connecting
bufferQuery = "initialize".getBytes();
bufferReply = new byte[256];
startClient();
}
/**
* Start client and allow user to make requests until they type 'exit'.
*/
public void startClient() {
System.out.println("Connection started... ");
// Send initial request
udpPacketRequest = new DatagramPacket(bufferQuery, bufferQuery.length, address, 5678);
try {udpSocket.send(udpPacketRequest);}
catch (IOException e) {e.printStackTrace();}
// Get initial inventory table
udpPacketResponse = new DatagramPacket(bufferReply, bufferReply.length);
try {udpSocket.receive(udpPacketResponse);}
catch (IOException e) {e.printStackTrace();}
System.out.println("Items in inventory: ");
System.out.println(new String(udpPacketResponse.getData(), 0, udpPacketResponse.getLength()));
System.out.println("Please input an id to make a query or 'exit' to close connection");
try {
while ((msgFromUser = systemInput.readLine()) != null) {
// send request for item details
bufferQuery = msgFromUser.getBytes();
udpPacketRequest = new DatagramPacket(bufferQuery, bufferQuery.length, address, 5678);
requestTime = new Timestamp(System.currentTimeMillis());
udpSocket.send(udpPacketRequest);
// get response
udpPacketResponse = new DatagramPacket(bufferReply, bufferReply.length);
udpSocket.receive(udpPacketResponse);
responseTime = new Timestamp(System.currentTimeMillis());
long elapsedTime = responseTime.getTime() - requestTime.getTime();
// display response
msgFromServer = new String(udpPacketResponse.getData(), 0, udpPacketResponse.getLength());
System.out.println(" Response: " + msgFromServer + " RTT of Query: " + elapsedTime + " msec ");
// exit option for user
if (msgFromUser.equals("exit")) {
System.out.println("Connection closing...");
break;
}
System.out.println("Please input an id to make a query or 'exit' to close connection");
}//end while
} catch (IOException e) {e.printStackTrace();}
udpSocket.close();
}//end startClient
}//end UDPClient
[8:26:46 AM] Learn and Earn: Edi only Part A
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.