Objective: WRITE A CLIENT SERVER PROGRAM WHICH DISPLAYS THE SERVER'S DATE AND TIME ON CLIENT MACHINE
MyClient.java
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
try {
Socket s = new Socket("localhost", 6666);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String serverDateTime = in.readLine();
System.out.println("Server's Date and Time: " + serverDateTime);
in.close();
s.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
MyServer.java
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept(); // establishes connection
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
// Get current date and time
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String serverDateTime = dateFormat.format(new Date());
// Send current date and time to client
out.println(serverDateTime);
ss.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
No comments:
Post a Comment