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

📄 serverwire.java

📁 利用BlueJ开发的一个类似小时候完的吃豆豆的小游戏
💻 JAVA
字号:
/* * Networked Wire, Server Side *  * $Id: ServerWire.java,v 1.2 2003/10/28 21:41:15 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.ServerDialog;import java.io.*;import java.net.*;/** * Networked Wire, Server Side.  Provides readObject, writeObject. * <p> * * If server's port is not provided at creation time, the user * is prompted for this information using ServerDialog. * <p> * Copyright 1996 Massachusetts Institute of Technology * * @see      cs101.net.Wire * @see      cs101.net.ClientWire * * @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: ServerWire.java,v 1.2 2003/10/28 21:41:15 gus Exp $ * */public class ServerWire 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;  /** Spew to System.out? */  private boolean verbose;    /**   * How to make one, if we know the port.  Control verbosity.   *   * @param port    the port number to listen on   * @param verbose toggle verbosity   */  public ServerWire( int port, boolean verbose ) {    this.verbose = verbose;    this.connectTo( port );  }      /**   * How to make one, if we know the port.  Verbose mode.   *   * @param port      the port number to listen on   */  public ServerWire( int port ) {    this(port, true);  }  /**   * How to make one, if we don't know who we want to talk to.   * Uses ServerDialog to ask user.  Verbose mode.   */  public ServerWire() {        this.verbose = true;    ServerDialog qd = new ServerDialog();    qd.ask();    this.connectTo( 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 port      the port number on which the server is listening   */  protected void connectTo( int port ) {    if (this.verbose) System.out.println("Server:  listening on port " + port);     try {      ServerSocket srv = new ServerSocket( port );      this.sock = srv.accept();      this.in = new ObjectInputStream( this.sock.getInputStream() );      this.out = new ObjectOutputStream( this.sock.getOutputStream() );    } catch (IOException e) {      throw new RuntimeException("Server:  " +				 "can't establish communication 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("ServerWire:  failed to read.");     }  }    /**   * 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("ServerWire:  failed to write.");     }  }    /**   *  Closes the Socket.   */    public void finalize() {    try {      this.in.close();      this.out.close();      this.sock.close();    } catch (IOException e) {}  }}/* Comments: * * History: *     $Log: ServerWire.java,v $ *     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.8  1999/02/17 17:36:47  tparnell *     added verbosity option * *     Revision 1.7  1999/01/20 22:28:05  tparnell *     left in a println.  oops * *     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:04  tparnell *     Fixed javadoc returns to return * *     Revision 1.4  1998/07/24 17:13:46  tparnell *     Placate new javadoc behavior * *     Revision 1.3  1998/07/23 14:58:15  tparnell *     javadoc fix * *     Revision 1.2  1998/07/22 18:18:08  tparnell *     move from util to net * *     Revision 1.1  1998/06/24 16:32:20  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 + -