Order the Book

Class NetworkClient

java.lang.Object
  |
  +--NetworkClient
Direct Known Subclasses:
HttpClient

public class NetworkClient
extends Object

A starting point for network clients. You'll need to override handleConnection, but in many cases connect can remain unchanged. It uses SocketUtil to simplify the creation of the PrintWriter and BufferedReader.

Taken from Core Servlets and JavaServer Pages from Prentice Hall and Sun Microsystems Press, http://www.coreservlets.com/. © 2000 Marty Hall; may be freely used or adapted.


Field Summary
protected  String host
           
protected  int port
           
 
Constructor Summary
NetworkClient(String host, int port)
          Register host and port.
 
Method Summary
 void connect()
          Establishes the connection, then passes the socket to handleConnection.
 String getHost()
          The hostname of the server we're contacting.
 int getPort()
          The port connection will be made on.
protected  void handleConnection(Socket client)
          This is the method you will override when making a network client for your task.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

host

protected String host

port

protected int port
Constructor Detail

NetworkClient

public NetworkClient(String host,
                     int port)
Register host and port. The connection won't actually be established until you call connect.
Method Detail

connect

public void connect()
Establishes the connection, then passes the socket to handleConnection.

handleConnection

protected void handleConnection(Socket client)
                         throws IOException
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.

getHost

public String getHost()
The hostname of the server we're contacting.

getPort

public int getPort()
The port connection will be made on.

Order the Book

Order
CSAJSP @ amazon.com

Return to Top-Level Source Code Archive