1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
/**
* Copyright 2001 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL
* WARRANTIES.
*/
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* A bare-bones Server containing a ServerSocket waiting for connection
* requests. Subclasses should implement the <code>spawnProtocolHandler</code>
* method.
*/
public abstract class TTSServer implements Runnable {
/**
* The port number to listen on. It is the value specified by the
* System property "port".
*/
protected int port = Integer.parseInt
(System.getProperty("port", String.valueOf(2222)));
/**
* Implements the run() method of Runnable interface. It starts a
* ServerSocket, listens for connections, and spawns a handler for
* each connection.
*/
public void run() {
ServerSocket ss;
try {
ss = new ServerSocket(port);
System.out.println("Waiting on " + ss);
} catch (IOException ioe) {
System.out.println("Can't open socket on port " + port);
ioe.printStackTrace();
return;
}
while (true) {
try {
Socket socket = ss.accept();
System.out.println("... new socket connection");
spawnProtocolHandler(socket);
} catch (IOException ioe) {
System.err.println("Could not accept socket " + ioe);
ioe.printStackTrace();
break;
}
}
try {
ss.close();
} catch (IOException ioe) {
System.err.println("Could not close server socket " + ioe);
ioe.printStackTrace();
}
}
/**
* This method is called after a connection request is made to this
* TTSServer. The <code>Socket</code> created as a result of the
* connection request is passed to this method.
*
* @param socket the socket
*/
protected abstract void spawnProtocolHandler(Socket socket);
}
|