Question: Please help us complete the following incomplete client code and server code to produce the outputs given in the picture and based on the assignment's

Please help us complete the following incomplete client code and server code to produce the outputs given in the picture and based on the assignment's requirements.
//CLIENT CODE: ------------------------
import java.io.*;
import java.net.*;
public class Client{
public static void main(String[] args){
DatagramSocket clientSocket = null;
try {
clientSocket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("localhost");
// Send request to the server with separator "_"
String request = "GET_TIMESTAMP";
byte[] sendData = request.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, 12345);
clientSocket.send(sendPacket);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String timestamp = new String(receivePacket.getData(),0, receivePacket.getLength());
System.out.println("Received timestamp from server: "+ timestamp);
} catch (IOException e){
e.printStackTrace();
} finally {
if (clientSocket != null && !clientSocket.isClosed()){
clientSocket.close();
}
}
}
}
//SERVER CODE: ------------------------
import java.io.*;
import java.net.*;
public class Server{
public static void main(String[] args){
DatagramSocket serverSocket = null;
try {
serverSocket = new DatagramSocket(12345);
byte[] receiveData = new byte[1024];
System.out.println("Server is listening on port 12345...");
while (true){
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String message = new String(receivePacket.getData(),0, receivePacket.getLength());
String[] parts = message.split("_");
if (parts.length ==2 && parts[0].equals("GET") && parts[1].equals("TIMESTAMP")){
String timestamp = java.time.LocalDateTime.now().toString();
InetAddress clientAddress = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
byte[] sendData = timestamp.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientAddress, clientPort);
serverSocket.send(sendPacket);
System.out.println("Timestamp sent to client at "+ clientAddress +":"+ clientPort);
} else {
System.out.println("Invalid request from client");
}
}
} catch (IOException e){
e.printStackTrace();
} finally {
if (serverSocket != null && !serverSocket.isClosed()){
serverSocket.close();
}
}
}
}
 Please help us complete the following incomplete client code and server

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!