Java Programming Home Page: Archive: Message #98

Date: Oct 25 2000 10:17:55 EDT
From: "Java Programming" <javaProgramming-owner@listbot.com>
Subject: How do you get the time from a socket?

Here is a nice program for getting the time
from a socket....For sw409 class....

import java.io.*;
import java.net.*;

public class NetUtil {
  public static String getTime() {
  	return getTime("www.docjava.com");
  }
	public static String getTime(String host) {
		String time = null;
  	Socket socket;
			try {

    			socket = new Socket(host, 13);

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

    			time = in.readLine();
  	}
  	catch (UnknownHostException e) {
    		e.printStackTrace();
  	}
  	catch (IOException e) {
   		e.printStackTrace();
     }
     return time;
}

public static void main(String[] args){
    System.out.println(getTime("www.docjava.com"));
}
}