Class SimpleNTPServer
java.lang.Object
org.apache.commons.net.examples.ntp.SimpleNTPServer
- All Implemented Interfaces:
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.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates SimpleNTPServer listening on default NTP port.SimpleNTPServer
(int port) Creates SimpleNTPServer. -
Method Summary
Modifier and TypeMethodDescriptionvoid
connect()
Connects to server socket and listen for client connections.int
getPort()
protected void
handlePacket
(DatagramPacket request, long rcvTime) Handles incoming packet.boolean
Returns state of whether time service is running.boolean
Returns state of whether time service is running.static void
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.
-
Constructor Details
-
SimpleNTPServer
public SimpleNTPServer()Creates SimpleNTPServer listening on default NTP port. -
SimpleNTPServer
Creates SimpleNTPServer.- Parameters:
port
- the local port the server socket is bound to, orzero
for a system selected free port.- Throws:
IllegalArgumentException
- if port number less than 0
-
-
Method Details
-
main
-
connect
Connects to server socket and listen for client connections.- Throws:
IOException
- if an I/O error occurs when creating the socket.
-
getPort
-
handlePacket
Handles incoming packet. If NTP packet is client-mode then respond to that host with a NTP response packet otherwise ignore.- Parameters:
request
- incoming DatagramPacketrcvTime
- time packet received- Throws:
IOException
- if an I/O error occurs.
-
isRunning
Returns state of whether time service is running.- Returns:
- true if time service is running
-
isStarted
Returns state of whether time service is running.- Returns:
- true if time service is running
-
run
Main method to service client connections. -
start
Starts time service and provide time to client connections.- Throws:
IOException
- if an I/O error occurs when creating the socket.
-
stop
Closes server socket and stop listening.
-