NetworkServerTest.java Establishes a network Server that listens for client requests on the port specified (command-line argument). Uses the following classes:
/** Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public class NetworkServerTest {
public static void main(String[] args) {
int port = 8088;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
}
NetworkServer nwServer = new NetworkServer(port, 1);
nwServer.listen();
}
}
# NetworkServer.java A starting point for network servers.
import java.net.*;
import java.io.*;
/** A starting point for network servers. You'll need to
* override handleConnection, but in many cases listen can
* remain unchanged. NetworkServer uses SocketUtil to simplify
* the creation of the PrintWriter and BufferedReader.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public class NetworkServer {
private int port, 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.
*/
public NetworkServer(int port, int maxConnections) {
setPort(port);
setMaxConnections(maxConnections);
}
/** Monitor a port for connections. Each time one is
* established, pass resulting Socket to handleConnection.
*/
public void listen() {
int i=0;
try {
ServerSocket listener = new ServerSocket(port);
Socket server;
while((i++ < maxConnections) || (maxConnections == 0)) {
server = listener.accept();
handleConnection(server);
}
} catch (IOException ioe) {
System.out.println("IOException: " + ioe);
ioe.printStackTrace();
}
}
/** 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.
*/
protected void handleConnection(Socket server)
throws IOException{
BufferedReader in = SocketUtil.getReader(server);
PrintWriter out = SocketUtil.getWriter(server);
System.out.println
("Generic Network Server: got connection from " +
server.getInetAddress().getHostName() + "\n" +
"with first line '" + in.readLine() + "'");
out.println("Generic Network Server");
server.close();
}
/** Gets the max connections server will handle before
* exiting. A value of 0 indicates that server should run
* until explicitly killed.
*/
public int getMaxConnections() {
return(maxConnections);
}
/** Sets max connections. A value of 0 indicates that server
* should run indefinitely (until explicitly killed).
*/
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
/** Gets port on which server is listening. */
public int getPort() {
return(port);
}
/** Sets port. You can only do before "connect" is
* called. That usually happens in the constructor.
*/
protected void setPort(int port) {
this.port = port;
}
}
SocketUtil.java Simplifies the creation of a PrintWriter and BufferedReader.
import java.net.*;
import java.io.*;
/** A shorthand way to create BufferedReaders and
* PrintWriters associated with a Socket.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public class SocketUtil {
/** Make a BufferedReader to get incoming data. */
public static BufferedReader getReader(Socket s)
throws IOException {
return(new BufferedReader(
new InputStreamReader(s.getInputStream())));
}
/** Make a PrintWriter to send outgoing data.
* This PrintWriter will automatically flush stream
* when println is called.
*/
public static PrintWriter getWriter(Socket s)
throws IOException {
// Second argument of true means autoflush.
return(new PrintWriter(s.getOutputStream(), true));
}
}
