Class SimpleNTPServer

  • All Implemented Interfaces:
    java.lang.Runnable

    public class SimpleNTPServer
    extends java.lang.Object
    implements java.lang.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 port  
      private boolean running  
      private java.net.DatagramSocket socket  
      private boolean started  
    • Constructor Summary

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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void connect()
      Connects to server socket and listen for client connections.
      int getPort()  
      protected void handlePacket​(java.net.DatagramPacket request, long rcvTime)
      Handles incoming packet.
      boolean isRunning()
      Returns state of whether time service is running.
      boolean isStarted()
      Returns state of whether time service is running.
      static void main​(java.lang.String[] args)  
      void run()
      Main method to service client connections.
      void start()
      Starts time service and provide time to client connections.
      void stop()
      Closes server socket and stop listening.
      • Methods inherited from class java.lang.Object

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

      • port

        private int port
      • running

        private volatile boolean running
      • started

        private boolean started
      • socket

        private java.net.DatagramSocket socket
    • Constructor Detail

      • SimpleNTPServer

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

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

      • main

        public static void main​(java.lang.String[] args)
      • connect

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

        public int getPort()
      • handlePacket

        protected void handlePacket​(java.net.DatagramPacket request,
                                    long rcvTime)
                             throws java.io.IOException
        Handles 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:
        java.io.IOException - if an I/O error occurs.
      • isRunning

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

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

        public void run()
        Main method to service client connections.
        Specified by:
        run in interface java.lang.Runnable
      • start

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

        public void stop()
        Closes server socket and stop listening.