⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientwire.java

📁 利用BlueJ开发的一个类似小时候完的吃豆豆的小游戏
💻 JAVA
字号:
/* * Networked Wire, Client Side *  * $Id: ClientWire.java,v 1.3 2003/11/03 19:00:06 gus Exp $ * * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the * CS101 homepage</a> or email <las@ai.mit.edu>. * * Copyright (C) 1996 Massachusetts Institute of Technology. * Please do not redistribute without obtaining permission. */package cs101.net;import cs101.awt.ClientDialog;import java.io.*;import java.net.*;/** * Networked Wire, Client Side.  Provides readObject, writeObject.  * * <P>If server's hostName and port are not provided at creation time, * the user is prompted for this information using ClientDialog. *  * <P>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 * * */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -