8 Replies - 1202 Views - Last Post: 07 June 2011 - 07:30 PM Rate Topic: -----

#1 dhow   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-June 11

Time server

Posted 06 June 2011 - 10:39 AM

Hey would like some help/advice on starting this time server program. I am trying to write a program that connects to a time server such as "time-a.nist.gov" and return the date as a string. Any help would be appreciated. thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: Time server

#2 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Time server

Posted 06 June 2011 - 01:37 PM

Show your code and the effort you have made and then we will be happy to give advise
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Time server

Posted 06 June 2011 - 01:57 PM

A bit of free advice for you: these things aren't simple request/response strings. They use Network Time Protocol. Read up on it there and here.

This post has been edited by Curtis Rutland: 06 June 2011 - 01:57 PM

Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Time server

Posted 06 June 2011 - 04:28 PM

You may want to check out tools like Sockets and URLs found in the java.net API.
Was This Post Helpful? 0
  • +
  • -

#5 dhow   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-June 11

Re: Time server

Posted 07 June 2011 - 07:29 AM

Here is the sample code that I have here. When ran from the command line you can input any web page and it will display the information. Now if I just run it with the my program(NETBEANS) it will print out the default web page information which is horstmann.com. I was thinking that I could use this code and have one of the time server web pages as the default but I just want it to print out the date and the time. Am I on the right track?
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
   This program demonstrates how to use a socket to communicate
   with a web server. Supply the name of the host and the
   resource on the command-line, for example
   java WebGet horstmann.com index.html
*/
public class WebGet
{
   public static void main(String[] args) throws IOException
   {
      // Get command-line arguments

      String host;
      String resource;

      if (args.length == 2)
      {
         host = args[0];
         resource = args[1];
      }
      else
      {
         System.out.println("Getting / from horstmann.com");
         host = "horstmann.com";
         resource = "/";
      }

      // Open socket

      final int HTTP_PORT = 80;
      Socket s = new Socket(host, HTTP_PORT);

      // Get streams
      
      InputStream instream = s.getInputStream();
      OutputStream outstream = s.getOutputStream();

      // Turn streams into scanners and writers

      Scanner in = new Scanner(instream);
      PrintWriter out = new PrintWriter(outstream);      

      // Send command

      String command = "GET " + resource + " HTTP/1.1\n" 
         + "Host: " + host + "\n\n";
      out.print(command);
      out.flush();

      // Read server response

      while (in.hasNextLine())
      {
         String input = in.nextLine();
         System.out.println(input);
      }

      // Always close the socket at the end

      s.close();      
   }

}


This post has been edited by macosxnerd101: 07 June 2011 - 07:30 AM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Time server

Posted 07 June 2011 - 07:55 AM

NTP isn't HTTP. You need to read the links I've provided.
Was This Post Helpful? 0
  • +
  • -

#7 dhow   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-June 11

Re: Time server

Posted 07 June 2011 - 08:38 AM

what if i used the daytime protocol?
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Time server

Posted 07 June 2011 - 07:18 PM

If you are a client you do not specify the port yourself.
You ask the server to provide you with the port to use for interprocess communication.

Never mind

This post has been edited by pbl: 07 June 2011 - 07:19 PM

Was This Post Helpful? 0
  • +
  • -

#9 supercorey   User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • Posts: 207
  • Joined: 15-February 09

Re: Time server

Posted 07 June 2011 - 07:30 PM

The entire point trying to be made here is that the code you have now is made for opening web-pages, which use the HTTP protocol. Time Servers use something called NTP which allows for accurate synchronization between the server and client. You can't open it like a web-page, you need to write code which utilizes the NTP protocol to fetch the time.
Curtis Rutland provided good links for learning about the NTP protocol and macosxnerd101 provided links about Java sockets and other network APIs which would be used. As has been said before, this isn't a simple sending of a "GET" or any other request string, this is a protocol that works differently than HTTP.

On your topic of the Daytime protocol, the Daytime protocol was not created for time synchronization, it is a now-obsolete way of testing network configurations. It would not be supported by modern time servers anyways.

This post has been edited by supercorey: 07 June 2011 - 07:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1