Answered step by step
Verified Expert Solution
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 clientserver scenario and based on the assignment's requirements.
CLIENT CODE:
import java.io;
import java.net.;
public class Client
public static void mainString args
DatagramSocket clientSocket null;
try
clientSocket new DatagramSocket;
InetAddress serverAddress InetAddress.getByNamelocalhost;
Send request to the server with separator
String request "GETTIMESTAMP";
byte sendData request.getBytes;
DatagramPacket sendPacket new DatagramPacketsendData sendDatalength, serverAddress, ;
clientSocket.sendsendPacket;
byte receiveData new byte;
DatagramPacket receivePacket new DatagramPacketreceiveData receiveData.length;
clientSocket.receivereceivePacket;
String timestamp new StringreceivePacketgetData receivePacket.getLength;
System.out.printlnReceived timestamp from server: timestamp;
catch IOException e
eprintStackTrace;
finally
if clientSocket null && clientSocket.isClosed
clientSocket.close;
SERVER CODE:
import java.io;
import java.net.;
public class Server
public static void mainString args
DatagramSocket serverSocket null;
try
serverSocket new DatagramSocket;
byte receiveData new byte;
System.out.printlnServer is listening on port ;
while true
DatagramPacket receivePacket new DatagramPacketreceiveData receiveData.length;
serverSocket.receivereceivePacket;
String message new StringreceivePacketgetData receivePacket.getLength;
String parts message.split;
if partslength && partsequalsGET && partsequalsTIMESTAMP
String timestamp java.time.LocalDateTime.nowtoString;
InetAddress clientAddress receivePacket.getAddress;
int clientPort receivePacket.getPort;
byte sendData timestamp.getBytes;
DatagramPacket sendPacket new DatagramPacketsendData sendDatalength, clientAddress, clientPort;
serverSocket.sendsendPacket;
System.out.printlnTimestamp sent to client at clientAddress : clientPort;
else
System.out.printlnInvalid request from client";
catch IOException e
eprintStackTrace;
finally
if serverSocket null && serverSocket.isClosed
serverSocket.close;
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