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.
Time server
Page 1 of 18 Replies - 1202 Views - Last Post: 07 June 2011 - 07:30 PM
Replies To: Time server
#2
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
#3
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
#4
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.
#5
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
#6
Re: Time server
Posted 07 June 2011 - 07:55 AM
NTP isn't HTTP. You need to read the links I've provided.
#7
Re: Time server
Posted 07 June 2011 - 08:38 AM
what if i used the daytime protocol?
#8
Re: Time server
Posted 07 June 2011 - 07:18 PM
You ask the server to provide you with the port to use for interprocess communication.
This post has been edited by pbl: 07 June 2011 - 07:19 PM
#9
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.
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
Page 1 of 1

New Topic/Question
Reply


MultiQuote







|