Question: Java Question. Implement a client-server program in which the client will print the date and time given by the server. Two classes should be implemented:

Java Question. Implement a client-server program in which the client will print the date and time given by the server. Two classes should be implemented: DateClient and DateServer. The DateServer simply prints new Date().toString() whenever it accepts a connection and then closes the socket using the starting program given. I did the program for DateClient and DateServer but I dont know how to add in to the ClientServerDemo.java given. Please help.

ClientServerDemo.java

import java.io.IOException; public class ClientServerDemo { public static void main(String[] args) throws InterruptedException { // start the server class ServerRunnable implements Runnable { public void run() { try { DateServer.main(new String[] {}); } catch (IOException ex) { ex.printStackTrace(); } } } Thread t1 = new Thread(new ServerRunnable()); t1.start(); // start the client class ClientRunnable implements Runnable { public void run() { try { DateClient.main(new String[] {}); } catch (IOException ex) { ex.printStackTrace(); } } } Thread t2 = new Thread(new ClientRunnable()); t2.start(); t2.join(); System.exit(0); } }

DateClient.java

import java.io.*;

import java.net.*;

class DateClient {

public static void main(String args[]) throws Exception

{

@SuppressWarnings("resource")

Socket soc= new Socket(InetAddress.getLocalHost(),5217);

BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));

System.out.println(in.readLine());

}

}

DateServer.java

import java.net.*;

import java.io.*;

import java.util.*;

class DateServer {

public static void main(String[] args) throws Exception

{

@SuppressWarnings("resource")

ServerSocket s = new ServerSocket(5217);

while(true)

{

System.out.println("Waiting for connection");

Socket soc = s.accept();

DataOutputStream out = new DataOutputStream(soc.getOutputStream());

out.writeBytes("Server Date: "+(new Date()).toString()+ " ");

out.close();

soc.close();

}

}

}

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!