Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to edit the given java codes to match the given scenario and to Ensure both client and server have logic to verify the validity

How to edit the given java codes to match the given scenario and to Ensure both client and server have logic to verify the validity of user inputs as per the guidelines and finally the server prints the timestamp and to print the email sent by client
Complete the following incomplete client code and server code to produce the outputs given in the client-server scenario 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();
}
}
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What are the need and importance of training ?

Answered: 1 week ago

Question

What is job rotation ?

Answered: 1 week ago