-----------------------------------------------
The One Class Server
-----------------------------------------------
EchoServer.java
-----------------------------------------------
-----------------------------------------------
import java.io.*;
import java.io.*;
public class EchoServer extends Server
{
// A single argument: the port on which to listen
public EchoServer( int port ) {
super( port );
}
// This function is called for each new connection; it
// implements whatever functionality is needed from the server.
// In this case, it simply sends back to the client all data
// that the client sends it. If it receives a 'q', the
// whole server is shut down.
public void process( InputStream in, OutputStream out ) {
try {
while (true) {
int c = in.read();
if (c=='q') {
close();
} else {
out.write( c );
}
}
} catch( IOException ie ) { System.out.println( ie ); }
}
// Command-line: "java EchoServer <port>"
static public void main( String args[] ) throws Exception {
int port = new Integer( args[0] ).intValue();
new EchoServer( port );
}
}