Skip to main content

request

Function request 

Source
pub fn request<A: ToSocketAddrs>(addr: A) -> Result<NtpResult>
Expand description

Send a blocking request to an NTP server with a hardcoded 5 second timeout.

This is a convenience wrapper around request_with_timeout with a 5 second timeout.

§Arguments

  • addr - Any valid socket address (e.g., "time.nist.gov:123" or "192.168.1.1:123")

§Returns

Returns an NtpResult containing the server’s response packet and computed timing information, or an error if the server cannot be reached or the response is invalid.

§Examples

// Request time from NTP server
let result = ntp::request("time.nist.gov:123")?;

// Access packet fields directly via Deref
println!("Server time: {:?}", result.transmit_timestamp);
println!("Stratum: {:?}", result.stratum);

// Access computed timing information
println!("Offset: {:.6} seconds", result.offset_seconds);
println!("Delay: {:.6} seconds", result.delay_seconds);

§Errors

Returns io::Error if:

  • Cannot bind to local UDP socket
  • Network timeout (5 seconds for read/write)
  • Invalid NTP packet response
  • DNS resolution fails
  • Response fails validation (wrong mode, origin timestamp mismatch, etc.)
  • Server sent a Kiss-o’-Death packet (see KissOfDeathError)