Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
58
Chapter 2 First Servlets
Listing 2.14 NetworkClient.java (continued)
  /** Establishes the connection, then passes the socket
   *  to handleConnection.
   *
   * @see #handleConnection
   */
  
  public void connect() {
    try {
      Socket client = new Socket(host, port);
      handleConnection(client);
    } catch(UnknownHostException uhe) {
      System.out.println("Unknown host: " + host);
      uhe.printStackTrace();
    } catch(IOException ioe) {
      System.out.println("IOException: " + ioe);
      ioe.printStackTrace();
    }
  }
  /** This is the method you will override when
   *  making a network client for your task.
   *  The default version sends a single line
   *  ("Generic Network Client") to the server,
   *  reads one line of response, prints it, then exits.
   */
  
  protected void handleConnection(Socket client)
      throws IOException {
    PrintWriter out =
      SocketUtil.getWriter(client);
    BufferedReader in =
      SocketUtil.getReader(client);
    out.println("Generic Network Client");
    System.out.println
      ("Generic Network Client:\n" +
       "Made connection to " + host +
       " and got '" + in.readLine() + "' in response");
    client.close();
  }
  /** The hostname of the server we're contacting. */
  
  public String getHost() {
    return(host);
  }
  /** The port connection will be made on. */
  
  public int getPort() {
    return(port);
  }
}
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