Question
In java Write a client program and a server program to implement the following features via using the UDP service. Client Program: 1. Display a
In java Write a client program and a server program to implement the following features via using the UDP service.
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 and read this user input as a string.
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 message 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.
The user input read in Step 1 is the destination address to which this request message is sent.
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 milliseconds.
6. Display a message on the standard output to ask the User whether to continue. If yes, repeat steps 2 through 5. Otherwise, close the socket and terminate the Client program.
here's my code is on the right track what else can I do?
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
// Display a message to ask the User to input the DNS or IP of the machine on which the Server Program runs
System.out.print("Enter the DNS or IP of the server: ");
String serverAddress = scanner.nextLine();
// Display the table
System.out.println("Item ID\t\tItem Description");
System.out.println("00001\t\tNew Inspiron 15");
System.out.println("00002\t\tNew Inspiron 17");
System.out.println("00003\t\tNew Inspiron 15R");
System.out.println("00004\t\tNew Inspiron 15z Ultrabook");
System.out.println("00005\t\tXPS 14 Ultrabook");
System.out.println("00006\t\tNew XPS 12 UltrabookXPS");
// Loop until user inputs a valid item ID
String itemID = "";
boolean validID = false;
while (!validID) {
System.out.print("Enter an item ID: ");
itemID = scanner.nextLine();
if (itemID.equals("00001") || itemID.equals("00002") || itemID.equals("00003") || itemID.equals("00004") ||
itemID.equals("00005") || itemID.equals("00006")) {
validID = true;
} else {
System.out.println("Invalid item ID. Please try again.");
}
}
DatagramSocket socket = new DatagramSocket();
byte[] buf = itemID.getBytes();
InetAddress address = InetAddress.getByName(serverAddress);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
// Record the local time right before sending the request
long startTime = System.currentTimeMillis();
// Send the request
socket.send(packet);
// Receive and interpret the response from the server
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
// Get the local time right after the response is received
long endTime = System.currentTimeMillis();
// Display the information
String received = new String(packet.getData(), 0, packet.getLength());
String[] parts = received.split(" ");
System.out.println("Item ID\t\tItem Description\t\tUnit Price\t\tInventory\t\tRTT of Query");
System.out.println(parts[0] + "\t\t" + parts[1] + "\t\t\t\t" + parts[2] + "\t\t" + parts[3] + "\t\t\t\t" + (endTime - startTime) + " ms");
// Ask the user whether to continue
System.out
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started