Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
16.12 A Debugging Web Server
429
Listing 16.11 NetworkServer.java 
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.
 *  @see SocketUtil
 */
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();
    }
  }
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved