ConnectionListener.java :  » Testing » jacareto » jacareto » cleverphl » connection » Java Open Source

Java Open Source » Testing » jacareto 
jacareto » jacareto » cleverphl » connection » ConnectionListener.java
/*
 * Jacareto Copyright (c) 2002-2005
 * Applied Computer Science Research Group, Darmstadt University of
 * Technology, Institute of Mathematics & Computer Science,
 * Ludwigsburg University of Education, and Computer Based
 * Learning Research Group, Aachen University. All rights reserved.
 *
 * Jacareto is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * Jacareto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Jacareto; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package jacareto.cleverphl.connection;


import jacareto.cleverphl.CleverPHL;

import java.io.IOException;

import java.net.ServerSocket;

/**
 * This class is a server lsitening to a certain port for incoming connection requests. When such a
 * request arrives, the server starts a new thread accepting this connection. It then continues to
 * listen for further requests.
 *
 * @author uak <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
 * @version 1.0
 */
public class ConnectionListener extends ServerSocket {
    /** The ConnectionThread for an incoming request */
    private ConnectionThread newThread;

    /** A flag indicating if the server is running */
    private boolean listening;

    /**
     * The default constructor. When created, the server listens for incoming connections on the
     * given port.
     *
     * @param cleverPHL the CleverPHL object this server socket belongs to
     * @param port
     *
     * @throws IOException
     */
    public ConnectionListener (CleverPHL cleverPHL, int port)
        throws IOException {
        super(port);
        listening = true;

        try {
            while (listening) {
                newThread = new ConnectionThread(accept (), cleverPHL);
                newThread.start ();
            }
        } catch (IOException e) {
            System.out.println ("Accept failed: " + port);
            System.exit (-1);
        }
    }

    /**
     * A method to close this socket
     */
    public void close () {
        listening = false;
        this.close ();
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.