ListenThread.java :  » Web-Server » tornado » tornado » Java Open Source

Java Open Source » Web Server » tornado 
tornado » tornado » ListenThread.java
// $Id: ListenThread.java,v 1.3 2001/01/17 01:47:12 nconway Exp $
package tornado;
import java.net.*;
import java.io.IOException;

public class ListenThread extends Thread {
    private final ServerPool serverPool;
    private final int port;
    private ServerSocket serverSocket;

    /** Constructs a new thread with the specified values.*/
    public ListenThread(ServerPool serverPool, int port) {
        this.serverPool = serverPool;
        this.port = port;
    }

    /** Begins an infinite loop, accepting and dispatching incoming
      * connections.
      */
    public void run() {
        bindToPort();
        while (true) {
            listen();
        }
    }

    /** Connects Tornado to a local system port. If an error is encountered,
      * Tornado aborts.
      */
    private void bindToPort() {
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            // treat all I/O errors as fatal
            throw new RuntimeException(e.getMessage());
        }
    }

    /** Waits for, accepts and dispatches a single incoming connection.*/
    private void listen() {
        try {
            Socket connection = serverSocket.accept();
            serverPool.dispatch(connection);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
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.