org.mitre.midiki.workshop
Class NetworkServer
java.lang.Object
|
+--org.mitre.midiki.workshop.NetworkServer
- Direct Known Subclasses:
- DiseaseServer
- public class NetworkServer
- extends java.lang.Object
A starting point for network servers. You'll need to
override handleConnection, but in many cases
listen can remain unchanged. It uses
SocketUtil to simplify the creation of the
PrintStream and DataInputStream.
This appears in Core Web Programming from
Prentice Hall Publishers, and may be freely used
or adapted. 1997 Marty Hall, hall@apl.jhu.edu.
- See Also:
SocketUtil
Constructor Summary |
NetworkServer(int port,
int maxConnections)
Build a server on specified port. |
Method Summary |
protected void |
handleConnection(java.net.Socket server)
This is the method that provides the behavior
to the server, since it determines what is
done with the resulting socket. |
void |
listen()
Monitor a port for connections. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
port
protected int port
maxConnections
protected int maxConnections
NetworkServer
public NetworkServer(int port,
int maxConnections)
- Build a server on specified port. It will continue
to accept connections (passing each to
handleConnection) until an explicit exit
command is sent (e.g. System.exit) or the
maximum number of connections is reached. Specify
0 for maxConnections if you want the server
to run indefinitely.
listen
public void listen()
- Monitor a port for connections. Each time one
is established, pass resulting Socket to
handleConnection.
handleConnection
protected void handleConnection(java.net.Socket server)
throws java.io.IOException
- This is the method that provides the behavior
to the server, since it determines what is
done with the resulting socket. Override this
method in servers you write.
This generic version simply reports the host
that made the connection, shows the first line
the client sent, and sends a single line
in response.