Package examples.ntp

Class SimpleNTPServer

java.lang.Object
examples.ntp.SimpleNTPServer
All Implemented Interfaces:
Runnable

public class SimpleNTPServer extends Object implements Runnable
The SimpleNTPServer class is a UDP implementation of a server for the Network Time Protocol (NTP) version 3 as described in RFC 1305. It is a minimal NTP server that doesn't actually adjust the time but only responds to NTP datagram requests with response sent back to originating host with info filled out using the current clock time. To be used for debugging or testing. To prevent this from interfering with the actual NTP service it can be run from any local port.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private int
     
    private boolean
     
     
    private boolean
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create SimpleNTPServer listening on default NTP port.
    SimpleNTPServer(int port)
    Create SimpleNTPServer.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Connect to server socket and listen for client connections.
    int
     
    protected void
    handlePacket(DatagramPacket request, long rcvTime)
    Handle incoming packet.
    boolean
    Return state of whether time service is running.
    boolean
    Return state of whether time service is running.
    static void
    main(String[] args)
     
    void
    run()
    main thread to service client connections.
    void
    Start time service and provide time to client connections.
    void
    Close server socket and stop listening.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • port

      private int port
    • running

      private volatile boolean running
    • started

      private boolean started
    • socket

      private DatagramSocket socket
  • Constructor Details

    • SimpleNTPServer

      public SimpleNTPServer()
      Create SimpleNTPServer listening on default NTP port.
    • SimpleNTPServer

      public SimpleNTPServer(int port)
      Create SimpleNTPServer.
      Parameters:
      port - the local port the server socket is bound to, or zero for a system selected free port.
      Throws:
      IllegalArgumentException - if port number less than 0
  • Method Details

    • getPort

      public int getPort()
    • isRunning

      public boolean isRunning()
      Return state of whether time service is running.
      Returns:
      true if time service is running
    • isStarted

      public boolean isStarted()
      Return state of whether time service is running.
      Returns:
      true if time service is running
    • connect

      public void connect() throws IOException
      Connect to server socket and listen for client connections.
      Throws:
      IOException - if an I/O error occurs when creating the socket.
    • start

      public void start() throws IOException
      Start time service and provide time to client connections.
      Throws:
      IOException - if an I/O error occurs when creating the socket.
    • run

      public void run()
      main thread to service client connections.
      Specified by:
      run in interface Runnable
    • handlePacket

      protected void handlePacket(DatagramPacket request, long rcvTime) throws IOException
      Handle incoming packet. If NTP packet is client-mode then respond to that host with a NTP response packet otherwise ignore.
      Parameters:
      request - incoming DatagramPacket
      rcvTime - time packet received
      Throws:
      IOException - if an I/O error occurs.
    • stop

      public void stop()
      Close server socket and stop listening.
    • main

      public static void main(String[] args)