alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Net example source code file (TimeTestSimpleServer.java)

This example Commons Net source code file (TimeTestSimpleServer.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Net tags/keywords

dataoutputstream, default_port, default_port, io, ioexception, ioexception, net, network, runnable, seconds_1900_to_1970, serversocket, serversocket, socket, timetestsimpleserver, timetestsimpleserver

The Commons Net TimeTestSimpleServer.java source code

package org.apache.commons.net.time;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * The TimetSimpleServer class is a simple TCP implementation of a server
 * for the Time Protocol described in RFC 868.
 * <p>
 * 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 <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc868.txt"> the spec  for
 * details.
 * <p>
 * Note this is for <B>debugging purposes only and not meant to be run as a realiable time service.
 *
 * @author Jason Mathews, MITRE Corporation
 *
 * @version $Revision: 1081478 $ $Date: 2011-03-14 17:12:48 +0000 (Mon, 14 Mar 2011) $
 */
public class TimeTestSimpleServer implements Runnable
{

    /**
     * baseline time 1900-01-01T00:00:00 UTC
     */
    public static final long SECONDS_1900_TO_1970 = 2208988800L;

    /*** The default time port.  It is set to 37 according to RFC 868. ***/
    public static final int DEFAULT_PORT = 37;

    private ServerSocket server;
    private int port;
    private boolean running = false;

    /**
     * Default constructor for TimetSimpleServer.
     * Initializes port to defaul time port.
     */
    public TimeTestSimpleServer()
    {
        port = DEFAULT_PORT;
    }

    /**
     * Constructor for TimetSimpleServer given a specific port.
     */
    public TimeTestSimpleServer(int port)
    {
        this.port = port;
    }

    public void connect() throws IOException
    {
        if (server == null)
        {
            server = new ServerSocket(port);
        }
    }

    public int getPort()
    {
        return server == null ? port : server.getLocalPort();
    }

    public boolean isRunning()
    {
        return running;
    }

    /**
     * Start time service and provide time to client connections.
     * @throws IOException
     */
    public void start() throws IOException
    {
        if (server == null)
    {
            connect();
    }
    if (!running)
    {
        running = true;
        new Thread(this).start();
    }
    }

    public void run()
    {
        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);
                    }
            }
        }
    }

    /**
     * Close server socket.
     */
    public void stop()
    {
        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;
        }
    }

    public static void main(String[] args)
    {
        TimeTestSimpleServer server = new TimeTestSimpleServer();
        try
        {
            server.start();
        } catch (IOException e)
        {
        }
    }

}

Other Commons Net examples (source code examples)

Here is a short list of links related to this Commons Net TimeTestSimpleServer.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.