import java.net.*; import java.io.*; import java.util.Date; public class TimeServer extends Thread { private int port; public TimeServer(int p) { port = p; } public void run() { try { ServerSocket serverSocket = new ServerSocket(port); System.out.println("Time Server started on port " + port); while (true) { Socket connection = serverSocket.accept(); sayTime(connection); connection.close(); } } catch (IOException e) {} } public void sayTime(Socket connection) throws IOException { OutputStream out = connection.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out)); Date date = new Date(); String strDate = date.toString(); writer.write(strDate); //writer.newLine(); writer.close(); } public static void main(String args[]) throws Exception { int p = Integer.parseInt(args[0]); TimeServer server = new TimeServer(p); server.start(); } }