package SocketExample;

// Fri Oct 15 18:07:43 EST 2004
//
// Written by Sean R. Owens, sean at guild dot net, released to the
// public domain.  Share and enjoy.  Since some people argue that it is
// impossible to release software to the public domain, you are also free
// to use this code under any version of the GPL, LPGL, or BSD licenses,
// or contact me for use of another license.
// http://darksleep.com/player

import java.net.Socket;
import java.net.ServerSocket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class SimpleServer {
    private int serverPort = 0;
    private ServerSocket serverSock = null;
    private Socket sock = null;

    public SimpleServer(int serverPort) throws IOException {
	this.serverPort = serverPort;

	serverSock = new ServerSocket(serverPort);
    }
    
    public void waitForConnections() {
	while (true) {
	    try {
		sock = serverSock.accept();
		System.err.println("SimpleServer:Accepted new socket, creating new handler for it.");
		SimpleHandler handler = new SimpleHandler(sock);
		handler.start();
		System.err.println("SimpleServer:Finished with socket, waiting for next connection.");
	    }
	    catch (IOException e){
		e.printStackTrace(System.err);
	    }
	}
    }

    public static void main(String argv[]) {
	int port = 54321;

	SimpleServer server = null;
	try {
	    server = new SimpleServer(port);
 	}
 	catch (IOException e){
	    e.printStackTrace(System.err);
 	}
	server.waitForConnections();
    }
}
