• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

understanding NTP in java

 
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
By reading how to make my java app get global time from some online clock?
I write code which fetch Date & Time from ntp server.
code is as follows:-


When i test it with 2 different conditions in computer which has Centos.
1. Service ntpd working.
2. Service ntpd stopped. change system date 1 year before (2014).

Both test i got same result.
Is there any way to get exception or error when ntpd manually stopped & this program run?

When i stop internet & run this program, at 40 sec i got expected exception as below:-
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would your application have to throw an exception when ntpd is not running?
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why would your application have to throw an exception when ntpd is not running?


As i wrote 'When i stop internet & run this program, at 40 sec i got expected exception'.
For testing i connect my pc to small basic network switch & in that attached internet Ethernet cable. When i remove internet cable. My pc cannot connect to Google etc website.
but 'Network connection' show me no problem.
So after that i run this program, & got exception.

My question is Internet working fine but i stopped ntp service from service manager or from system datetime configuration.
Then why not my program give me error. It show me datetime same as system datetime.

I read about NTPUDPClient as below:-


/***
* The NTPUDPClient class is a UDP implementation of a client for the
* Network Time Protocol (NTP) described in RFC 1305 as well as the
* Simple Network Time Protocol (SNTP) in RFC-2030. To use the class,
* merely open a local datagram socket with open
* and call getTime to retrieve the time. Then call
* close
* to close the connection properly.
* Successive calls to getTime are permitted
* without re-establishing a connection. That is because UDP is a
* connectionless protocol and the Network Time Protocol is stateless.
*
* @author Jason Mathews, MITRE Corp
* @version $Revision: 1299238 $
***/



Is there any way that use TCP or something for NTP so when actually when we try to connect & it cant reach there, then program show some error or exception?
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should probably Google what ntpd is and figure out why it's not related to how your application operates.
 
Saloon Keeper
Posts: 28573
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NTP isn't nearly as simple as you might think. Among other things, it's designed to accept time inputs from multiple sources. Typically several Internet timeservers or server pools, but also locally-attached GPS or atomic clocks, WWVB receivers, and so forth. NTP then takes up all this information and attempts to construct a consensus as to what the actual local time is. Subject to some blurring since the system it's running on has internal latencies.

You don't talk to NTP directly. The NTP daemon adjusts the system clock, so the ordinary system time functions are what you use to get the time, whether NTPD is running or not.

Microsoft Windows LAN users have also been able to have their system clocks adjusted by the Microsoft LAN itself when you login to the LAN.
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim Holloway & Stephan van Hulst,

You should probably Google what ntpd is and figure out why it's not related to how your application operates.


I search about NTP & SNTP, & find out my query will be solve by SNTP(might be).

You don't talk to NTP directly. The NTP daemon adjusts the system clock, so the ordinary system time functions are what you use to get the time, whether NTPD is running or not.


In Centos whenever i edit Adjust Date & time by GUI(by right click on clock shown on Desktop), I see 'service ntpd status' as stopped. example I try to set date as 1-Jan-1980, i have to stop NTP then i can change date.
When i select NTP, service ntpd status shows running. So i thought there is something connection between system clock & service ntpd.
My actual aim is
To check system time is really sync with internet or not, before my program start doing actual functionality.
I found one more code which use SNTP. & might be by this i solve lots of possibilities.
2 programs, MySNTP.java & NtpMessage.java

When i connect my pc to network switch which has Internet connection. & Then test this program.
1. Stop ntpd service but internet working fine.
2. Internet connection removed from switch but PC network & ntpd service is fine.

When i test 1st condition, I got result as


When i test 2nd condition i got result


My question is
1. All my exercise of checking my pc is sync with internet or not, is right way or wrong way?

For reference both codes are as follows:-



 
Tim Holloway
Saloon Keeper
Posts: 28573
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. Long code samples tend to go unread here. It's the downside of free support.

The master timekeeper for a computer is the clock chip that's part of the core motherboard hardware. This is what gets queried when you do an OS function call for time. The clock runs as an independent processor, so it doesn't get affected by, nor does it affect CPU loads.

NTP is NOT directly queried for time. First of all, because the CPU and network overhead would be prohibitive, secondly because network response times can vary wildly, so getting an accurate query is more of a statistical function than an absolute point.

As I said before, the NTP server usually gets time from multiple sources and forms a consensus time which it then uses to periodically correct the hardware clock. Because the hardware clock is usually not very good and so it drifts off of the "true" time unless a more accurate source keeps it corrected.

Here's an analogy. Suppose you have 2 clocks. One is a battery-powered digital crystal (non-atomic) clock. The other is one of the old-time LED clocks that you have to set yourself but it runs off the power line. Which do you think is more accurate?

Trick answer! The A/C power line is noisy, sloppy and slow (typically 50/60 cycles per second). But its overall accuracy is more reliable, since all of the power companies on the grid MUST keep their powerline cycles in sync. Otherwise one could be pushing a positive voltage over the wire at the same time another was in the negative phase - effectively a high-power short circuit. The crystal clock will be more accurate in the short term, but any drift will not be corrected, so eventually it could end up off by a very long amount. And a crystal clock is what your computer uses.

So it isn't necessary to have NTP running all the time, just enough to keep the CPU clock on track.

I don't recall CentOS restarting NTP if one resets the clock manually, but it is definite that NTP shouldn't be expected to do really coarse-grained time setting. NTP attempts to make fine adjustments, so if your clock is off by more than an hour, you need to manually reset it to within a minute or so of the correct time.

You should be able to see diagnostic information in your syslog that tells how well NTP is tracking the absolute time. If you don't, check the NTP config file options for logging settings.

And if you haven't visited This Site, then I encourage you to do so!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic