DayTime Service

The Daytime Protocol (Internet RFC 867) is a simple protocol that allows clients to retrieve the current date and time from a remote server. While useful at a bsic level, the Daytime protocol is most often used for debugging purposes rather than actually acquire the current date and time.

The daytime protocol is available on well-known TCP port 13.

Network Time Protocol (NTP) (Internet RFC-1305a) is an Internet standard protocol which enables client computers to maintain system time synchronization to the US Naval Observatory Master Clocks in Washington, DC and Colorado Springs, CO.

NTP runs as a client program on a computer. It sends periodic time requests to one or more servers, obtaining server time stamps and using them to adjust the client's clock.

Typical accuracy achieved is in the range 1 - 30 ms continuous, and is highly dependent on the symmetry and speed of the Internet path between client and server. Best results are achieved using a combination of servers which are closest to the client in a network sense.

Reference

DayTimeClient.java

//----------------------------------------------------------------------
// File:   DayTimeClient
// What:   A TCP Client for the DayTime service on port 13
// Who:    Dick Steflik (steflik@binghamton.edu)
//----------------------------------------------------------------------
 
import java.io.*;
import java.net.*;

public class DayTimeClient
{
   public static final int port = 13;
   public static void main(String args[])
   {
     Socket s = null;
     String timestamp;
	 // Possible servers:
	 //   tock.usno.navy.mil 
	 //   ntp2.usno.navy.mil 

     try
       {
         // create the socket to the remote host
         s = new Socket((args.length>0? args[0]: "tock.usno.navy.mil") , port);
         // create an input stream and tie it to the socket
         InputStream in = s.getInputStream();
         BufferedReader sin = new BufferedReader(
                                  new InputStreamReader(in)); 
         // tell user they are connected 
         System.out.println("Connected to: " +
              s.getInetAddress() + " on port " + s.getPort()) ;
         while (true)
          {
            // read the timstamp
            timestamp = sin.readLine();
            if (timestamp == null)
              { 
                System.out.println("Server closed connection");
                break;
              } 
            System.out.println("Daytime :" + timestamp);
           }
       }
     catch (IOException e)
       { System.out.println(e);}
     finally
       {
         // force the connection closed in case its still open
         try
           { if ( s != null)  s.close();}
     
         catch (IOException e2)
           { }
       }
   }
}


Maintained by John Loomis, last updated 5 Jan 2001