If server's hostName and port are not provided at creation time, * the user is prompted for this information using ClientDialog. * *
Copyright 1996 Massachusetts Institute of Technology * * @see cs101.net.Wire * @see cs101.net.ServerWire * * @author Todd C. Parnell, tparnell@ai.mit.edu * @author Maciej Stachowiak, maciej@ai.mit.edu * @author Lynn Andrea Stein, las@ai.mit.edu * @version $Id: ClientWire.java,v 1.3 2003/11/03 19:00:06 gus Exp $ */ public class ClientWire implements Wire { /** * The network connection. Contains all of the relevant connect * info, if we need it */ private Socket sock; /** Where to read from */ private ObjectInputStream in; /** Where to write to */ private ObjectOutputStream out; /** Who we're connected to */ private String hostname; /** Which port we're connected to */ private int port; /** Operate verbosely */ private boolean verbose; /** * How to make one, if we know who we want to talk to. * Specify verboseness. * * @param hostName the name of the machine that the server is on * @param port the port number on which the server is listening * @param verbose specify verboseness level */ public ClientWire( String hostName, int port, boolean verbose ) { this.verbose = verbose; this.connectTo( hostName, port ); } /** * How to make one, if we know who we want to talk to. * Verbosness on. * * @param hostName the name of the machine that the server is on * @param port the port number on which the server is listening */ public ClientWire( String hostName, int port ) { this(hostName, port, true); } /** * How to make one, if we don't know who we want to talk to. * Uses ClientDialog to ask user. Verboseness on. */ public ClientWire() { this.verbose = true; ClientDialog qd = new ClientDialog(); qd.ask(); this.connectTo( qd.getHostName(), qd.getPort() ); } /** * Opens a connection to a server presumed to be listening on hostName, * port. Sets up listener thread. Called by constructor; should not be * called otherwise. * * @param hostName the name of the machine that the server is on * @param port the port number on which the server is listening */ protected void connectTo( String hostName, int port ) { if (this.verbose) System.out.println("Client: trying to connect to " + hostName + " on port " + port ); try { this.hostname = hostName; this.port = port; this.sock = new Socket( hostName, port ); this.out = new ObjectOutputStream( this.sock.getOutputStream() ); this.in = new ObjectInputStream( this.sock.getInputStream() ); } catch (IOException e) { throw new RuntimeException("Client: " + "can't establish communication with " + hostName + " on port " + port ); } } /** * Use this to read an Object from the Wire. * * @return the Object read. */ public Object readObject() { try { return this.in.readObject(); } catch (Exception e) { throw new RuntimeException("ClientWire: failed to read from " + hostname + " on " + port); } } /** * Use this method to write an Object to the Wire. * * @param o The object to be written. */ public void writeObject( Object o ) { try { this.out.writeUnshared( o ); } catch (IOException e) { throw new RuntimeException("ClientWire: failed to write to " + hostname + " on " + port); } } /** * Closes the Socket and Streams. */ public void finalize() { try { this.in.close(); this.out.close(); this.sock.close(); } catch (IOException e) {} } } /* Comments: * * History: * $Log: ClientWire.java,v $ * Revision 1.3 2003/11/03 19:00:06 gus * fix typo * * Revision 1.2 2003/10/28 21:41:15 gus * use writeUnshared not writeObject to avoid confusing communication problems * * Revision 1.1.1.1 2002/06/05 21:56:32 root * CS101 comes to Olin finally. * * Revision 1.7 1999/02/17 20:24:22 tparnell * added verbose option * * Revision 1.6 1999/01/20 22:24:55 tparnell * Modified ordering on creation of input/outputStreams * * Revision 1.5 1998/10/16 19:46:02 tparnell * Fixed javadoc returns to return * * Revision 1.4 1998/07/24 17:13:39 tparnell * Placate new javadoc behavior * * Revision 1.3 1998/07/22 18:17:53 tparnell * move from util to net * * Revision 1.2 1998/06/24 21:28:53 tparnell * code cleanup * * Revision 1.1 1998/06/24 16:32:16 tparnell * changes after summer98. added a multi-user Group server and some * other misc files * * */