// This appears in Core Web Programming from // Prentice Hall Publishers, and may be freely used // or adapted. 1997 Marty Hall, hall@apl.jhu.edu. import java.net.*; import java.io.*; /** 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 * PrintStream and DataInputStream. * @see SocketUtil */ public class NetworkClient { protected String host; protected int port; //---------------------------------------------------- /** Register host and port. The connection won't * actually be established until you call * connect. * * @see #connect */ public NetworkClient(String host, int port) { System.out.println("NetworkClient constructor"); this.host = host; this.port = port; } //---------------------------------------------------- /** Establishes the connection, then passes the socket * to handleConnection. * * @see #handleConnection */ public void connect() { try { Socket client = new Socket(host, port); handleConnection(client); System.out.println("Network handleconnection"); } catch(UnknownHostException uhe) { System.out.println("Unknown host: " + host); uhe.printStackTrace(); } catch(IOException ioe) { System.out.println("IOException: " + ioe); ioe.printStackTrace(); } System.out.println("connection finished"); } //---------------------------------------------------- /** 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 { SocketUtil s = new SocketUtil(client); PrintStream out = s.getPrintStream(); DataInputStream in = s.getDataStream(); 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); } //---------------------------------------------------- }