FileDocCategorySizeDatePackage
Server.javaAPI DocExample8038Thu Aug 08 12:44:04 BST 1996None

Server

public class Server extends Thread

Fields Summary
public static final int
DEFAULT_PORT
protected int
port
protected ServerSocket
listen_socket
protected ThreadGroup
threadgroup
protected List
connection_list
protected Vector
connections
protected Vulture
vulture
Constructors Summary
public Server(int port)

        // Create our server thread with a name.
        super("Server");
        if (port == 0) port = DEFAULT_PORT;
        this.port = port;
        try { listen_socket = new ServerSocket(port); }
        catch (IOException e) { fail(e, "Exception creating server socket"); }
        // Create a threadgroup for our connections
        threadgroup = new ThreadGroup("Server Connections");

        // Create a window to display our connections in
        Frame f = new Frame("Server Status");
        connection_list = new List();
        f.add("Center", connection_list);
        f.resize(400, 200);
        f.show();

        // Initialize a vector to store our connections in
        connections = new Vector();

        // Create a Vulture thread to wait for other threads to die.
        // It starts itself automatically.
        vulture = new Vulture(this);

        // Start the server listening for connections
        this.start();
    
Methods Summary
public static voidfail(java.lang.Exception e, java.lang.String msg)

    
    // Exit with an error message, when an exception occurs.
           
        System.err.println(msg + ": " +  e);
        System.exit(1);
    
public static voidmain(java.lang.String[] args)

        int port = 0;
        if (args.length == 1) {
            try { port = Integer.parseInt(args[0]); }
            catch (NumberFormatException e) { port = 0; }
        }
        new Server(port);
    
public voidrun()

        try {
            while(true) {
                Socket client_socket = listen_socket.accept();
                Connection c = new Connection(client_socket, threadgroup,
                                  3, vulture);
                // prevent simultaneous access.
                synchronized (connections) {
                    connections.addElement(c);
                    connection_list.addItem(c.toString());
                }
            }
        }
        catch (IOException e) {
            fail(e, "Exception while listening for connections");
        }