-4

I want to do time request on an socket client. According to this time.nist.gov is on port 13

Does time.google.com have a similar service so that I can connect a socket client to it. just like I can connect to time.nist.gov on port 13

I wanted to do something like this

import java.net.*;
import java.io.*;
 
/**
 * This program is a socket client application that connects to a time server
 * to get the current date time.
 *
 * @author www.codejava.net
 */
public class TimeClient {
 
    public static void main(String[] args) {
        String hostname = "time.google.com";
        int port = 13;
 
        try (Socket socket = new Socket(hostname, port)) {
 
            InputStream input = socket.getInputStream();
            InputStreamReader reader = new InputStreamReader(input);
 
            int character;
            StringBuilder data = new StringBuilder();
 
            while ((character = reader.read()) != -1) {
                data.append((char) character);
            }
 
            System.out.println(data);
 
 
        } catch (UnknownHostException ex) {
 
            System.out.println("Server not found: " + ex.getMessage());
 
        } catch (IOException ex) {
 
            System.out.println("I/O error: " + ex.getMessage());
        }
    }
}

1 Answers1

3

If they offer daytime, it will be on port 13. But daytime is archaic at best, and has nearly no legitimate uses today. time.nist.gov probably offers it for archaic reasons.

Google on the other hand tends to push forward new standards, and abandons bad standards quickly. Daytime is an example of a bad standard that has been superseded by something much better: ntp.

I can not find any documentation hinting that google supports daytime, and time.google.com does not respond on port 13. So they probably does not support daytime at all.

You should not use daytime at all in a new development. It's a protocol that is not in regular use, and will disappear. Use ntp or sntp. There's sntp or ntp libraries for every imaginable platform.

If your worry is that ntp libraries carry an overhead, you should really read the specification for daytime - which is from 1983! Yikes!

Daytime Syntax

   There is no specific syntax for the daytime.  It is recommended that
   it be limited to the ASCII printing characters, space, carriage
   return, and line feed.  The daytime should be just one line.

      One popular syntax is:

         Weekday, Month Day, Year Time-Zone

         Example:

            Tuesday, February 22, 1982 17:37:43-PST

They also note that time is an better protocol. Time sends time in a machine readable format: seconds since 12:00:00 1900-01-01. Not exactly trivial to translate that into todays date either, so you will be likely to require a library. But noone used time in the past 20 years, so such libraries are likely to be scarce.

NTP on the other hand...

vidarlo
  • 6,654
  • 2
  • 18
  • 31
  • alright let me look into ntp and sntp i didnt want to use libraries though – Bret Joseph Jan 01 '23 at 12:21
  • See my edited answer. Daytime was never intended as machine readable anyway. And you're likely to need a library of some sort to do your deeds... – vidarlo Jan 01 '23 at 12:37