1. 1. If the NTP server is not reachable then it exits the script and i never get any error message as such. Can i add error handling here ?

    A quick look inside the module shows that if any error occurs; the module dies with an error.

    So, you can either allow that error to be displayed directly; or you could use block eval to trap it within your script:

    #! perl -slw use strict; use Data::Dump qw[ pp ]; use Net::NTP qw[ get_ntp_response ]; my %resp; eval{ %resp = get_ntp_response( '0.poll.ntp.org' ); 1; } or die $@; pp \%resp;

    Not that there would be much point if all you are going to do is die yourself; but you get the point.

  2. If the way i am trying to get time the right way ?

    This is a more complex question.

    The reason for having the four timestamps in the response, is intended to allow the user to access the correctness of time returned.

    In a typical repsonse:

    C:\test>ntp { "Leap Indicator" => 0, Mode => 4, "Originate Timestamp" => "1338194114.66406", "Poll Interval" => "0.0000", Precision => -22, "Receive Timestamp" => "1338194119.80861", "Reference Clock Identifier" => "195.66.241.3", "Reference Timestamp" => "1338192423.2336", "Root Delay" => "0.00360107421875", "Root Dispersion" => "0.0000", Stratum => 2, "Transmit Timestamp" => "1338194119.80867", "Version Number" => 3, }

    You can see by subtracting the time when the server recieved the request ("Receive Timestamp") from the time the client included in the request ("Originate Timestamp") that the packet took a little over 5 seconds to reach the server.

    So, the server has factored that in, so that when it sends the response at ("Transmit Timestamp") 1338194119.80867, it adds that delay to the ("Reference Timestamp"), 1338192423.2336, so by the time you receive it, it should be pretty close to spot on.

    Of course, if you delay utilising it at your end -- say, by letting it get caught up in interminable layers of some redundant behemoth of an IO library like POE -- then your just throwing away all work the NTP protocol uses to try and achieve accuracy.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


In reply to Re: NTP Error by BrowserUk
in thread NTP Error by saeen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.