org.jscience.net.ntp
Class NtpConnection

java.lang.Object
  extended by org.jscience.net.ntp.NtpConnection

public class NtpConnection
extends java.lang.Object

This class encapsulates the exchange of a NtpDatagram between a client and a server.


Field Summary
static int defaultNtpPort
          Default port for the NTP protocol.
 
Constructor Summary
NtpConnection(java.net.InetAddress iaddr)
          Creates a UDP connection for the exchange of NtpDatagrams.
NtpConnection(java.net.InetAddress iaddr, int iport)
          Creates a UDP connection for the exchange of NtpDatagrams.
 
Method Summary
 void close()
          Close the connection.
 void finalize()
          Could be used to do some cleaning up.
 NtpInfo getInfo()
          Obtain info from the server.
 java.util.Date getTime()
          Get the time from the server.
 int getTimeout()
          Get the timeout associated with the connection.
 java.util.Vector getTrace()
          Traces a server to the primary server.
 void receive(NtpDatagramPacket ntpDatagramPacket)
          Wait for a reply from the server.
 void send(NtpDatagramPacket ntpDatagramPacket)
          Send a NtpDatagram to the server.
 void setTimeout(int timeout)
          Set the timeout associated with the connection.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultNtpPort

public static final int defaultNtpPort
Default port for the NTP protocol.

See Also:
Constant Field Values
Constructor Detail

NtpConnection

public NtpConnection(java.net.InetAddress iaddr,
                     int iport)
              throws java.net.SocketException
Creates a UDP connection for the exchange of NtpDatagrams.

Parameters:
iaddr - DOCUMENT ME!
iport - DOCUMENT ME!
Throws:
java.net.SocketException - DOCUMENT ME!

NtpConnection

public NtpConnection(java.net.InetAddress iaddr)
              throws java.net.SocketException
Creates a UDP connection for the exchange of NtpDatagrams.

Parameters:
iaddr - DOCUMENT ME!
Throws:
java.net.SocketException - DOCUMENT ME!
Method Detail

getTimeout

public int getTimeout()
Get the timeout associated with the connection. The default timeout is 10s.

Returns:
DOCUMENT ME!

setTimeout

public void setTimeout(int timeout)
                throws java.net.SocketException
Set the timeout associated with the connection.

Parameters:
timeout - DOCUMENT ME!
Throws:
java.net.SocketException - DOCUMENT ME!

send

public void send(NtpDatagramPacket ntpDatagramPacket)
          throws java.io.IOException
Send a NtpDatagram to the server.

Parameters:
ntpDatagramPacket - DOCUMENT ME!
Throws:
java.io.IOException - DOCUMENT ME!

receive

public void receive(NtpDatagramPacket ntpDatagramPacket)
             throws java.io.IOException
Wait for a reply from the server.

Parameters:
ntpDatagramPacket - DOCUMENT ME!
Throws:
java.io.IOException - A IOException is thrown in case of a timeout.

getInfo

public NtpInfo getInfo()
                throws java.io.IOException
Obtain info from the server.

Returns:
DOCUMENT ME!
Throws:
java.io.IOException - A IOException is thrown in case of a timeout.
See Also:
NtpInfo

getTrace

public java.util.Vector getTrace()
Traces a server to the primary server.

Returns:
Vector containing the NtpInfo objects associated with the servers on the path to the primary server. Sometimes only a partial list will be generated due to timeouts or other problems.

getTime

public java.util.Date getTime()
Get the time from the server.

Returns:
A Date object containing the server time, adjusted for roundtrip delay. Note that it is better to use getInfo() and then to use the offset field of the returned NtpInfo object.

close

public void close()
Close the connection.


finalize

public void finalize()
Could be used to do some cleaning up. The default implementation just invokes close().

Overrides:
finalize in class java.lang.Object