FileDocCategorySizeDatePackage
TimeTestSimpleServer.javaAPI DocApache Commons NET 1.4.1 API6332Sat Dec 03 10:05:48 GMT 2005org.apache.commons.net.time

TimeTestSimpleServer

public class TimeTestSimpleServer extends Object implements Runnable
The TimetSimpleServer class is a simple TCP implementation of a server for the Time Protocol described in RFC 868.

Listens for TCP socket connections on the time protocol port and writes the local time to socket outputStream as 32-bit integer of seconds since midnight on 1 January 1900 GMT. See the spec for details.

Note this is for debugging purposes only and not meant to be run as a realiable time service.

author
Jason Mathews, MITRE Corporation
version
$Revision: 1.1 $ $Date: 2003/09/24 21:51:48 $

Fields Summary
public static final long
SECONDS_1900_TO_1970
baseline time 1900-01-01T00:00:00 UTC
public static final int
DEFAULT_PORT
The default time port. It is set to 37 according to RFC 868.
private ServerSocket
server
private int
port
private boolean
running
Constructors Summary
public TimeTestSimpleServer()
Default constructor for TimetSimpleServer. Initializes port to defaul time port.


                   
     
    
        port = DEFAULT_PORT;
    
public TimeTestSimpleServer(int port)
Constructor for TimetSimpleServer given a specific port.

        this.port = port;
    
Methods Summary
public voidconnect()

        if (server == null)
        {
            server = new ServerSocket(port);
        }
    
public intgetPort()

        return server == null ? port : server.getLocalPort();
    
public booleanisRunning()

        return running;
    
public static voidmain(java.lang.String[] args)

        TimeTestSimpleServer server = new TimeTestSimpleServer();
        try
        {
            server.start();
        } catch (IOException e)
        {
        }
    
public voidrun()

        Socket socket = null;
        while (running)
        {
            try
            {
                socket = server.accept();
                DataOutputStream os = new DataOutputStream(socket.getOutputStream());
                // add 500 ms to round off to nearest second
                int time = (int) ((System.currentTimeMillis() + 500) / 1000 + SECONDS_1900_TO_1970);
                os.writeInt(time);
                os.flush();
            } catch (IOException e)
            {
            } finally
            {
                if (socket != null)
                    try
                    {
                        socket.close();  // force closing of the socket
                    } catch (IOException e)
                    {
                        System.err.println("close socket error: " + e);
                    }
            }
        }
    
public voidstart()
Start time service and provide time to client connections.

throws
IOException

        if (server == null)
	{
            connect();
	}
	if (!running)
	{
	    running = true;
	    new Thread(this).start();
	}
    
public voidstop()
Close server socket.

        running = false;
        if (server != null)
        {
            try
            {
                server.close();  // force closing of the socket
            } catch (IOException e)
            {
                System.err.println("close socket error: " + e);
            }
            server = null;
        }