import java.net.*;
import java.io.*;

public class MTServerBase extends Thread {
    // client
    protected Socket socket;

    // Here is the thread that does the work
    // Presumably you'll override this in the subclass
    public void run() {
        try {
          String s = "I'm a test server. Goodbye";
          socket.getOutputStream().write(s.getBytes());
          socket.close();
          }
        catch (Exception e) {
          System.out.println(e);
          }
        }

    static public void startServer(int port,Class clobj) {
	ServerSocket ssock;
	Socket sock;
        try {
	    ssock=new ServerSocket(port);
	    while (true) {
		Socket esock=null;
		try {
		    esock=ssock.accept();
// create new MTServerBase or subclass
		    MTServerBase t=(MTServerBase)clobj.newInstance(); 
                    t.socket=esock;
                    t.start();
		} catch (Exception e) { 
		    try { esock.close(); } catch (Exception ec) {}
		}
	    }
	} catch (IOException e) {
	}
	// if we return something is wrong!
    }


// Very simple test main
    static public void main(String args[]) {
	System.out.println("Starting server on port 808");
	MTServerBase.startServer(808,MTServerBase.class);
      }
    }
