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

📄 babysitter.java

📁 利用BlueJ开发的一个类似小时候完的吃豆豆的小游戏
💻 JAVA
字号:
/* * cs101.net.BabySitter.java * $Id: BabySitter.java,v 1.2 2003/09/23 15:18:08 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) 1998 Massachusetts Institute of Technology. * Please do not redistribute without obtaining permission. */package cs101.net;import java.io.*;import java.net.*;/** * This object handles the io between the server and one client. * It has a run method that listens for information and relays it to * the server.   It also has a method to send information back to its client. * * <P>Copyright 1998 Massachusetts Institute of Technology * * @author Todd C. Panrell, tparnell@ai.mit.edu * @version $Id: BabySitter.java,v 1.2 2003/09/23 15:18:08 gus Exp $ * */public class BabySitter implements Runnable {  /** Server this client is connected to */  protected Server server;  /** Animacy that runs this object */  protected Thread spirit;  /** Client connection */  protected Socket sock;  /** Where to read from client */  protected ObjectInputStream ois;  /** Where to write to client */  protected ObjectOutputStream oos;  /** Flag to indicate whether the Thread animating this should stop */  private boolean stopped;  /**    * Creates a BabySitter to handle communication with a    * client.  Spawns a new thread to handle input from client;   * sends strings on request.   *    * @param sock the client socket of this threads client   * @param server The <code>Server</code> object for this BabySitter   *   * @see #send   * @see Server#sendToAllExcept   */  protected BabySitter(Socket sock, Server server) {    System.out.println( "Server:  setting up new connection from " +			sock.getInetAddress().getHostName() +			" on port " + sock.getPort() );    this.sock = sock;    this.server = server;    try {      // it is very, very important to create the Output before Input      this.oos = new ObjectOutputStream(sock.getOutputStream());      this.ois = new ObjectInputStream(sock.getInputStream());    } catch (IOException e) {      try { this.sock.close(); } catch ( IOException e2 ) {}      System.out.println("Server:  socket error in BabySitter");      this.server.removeBabySitter(this);      return;    }    this.spirit = new Thread( this );    this.spirit.start();  }  /**   * Sends a String to the client.   *   * @param s the String to send   */  protected void send(String s) {    try {      //	    System.out.println("Server:  sending "+s);      this.oos.writeObject(s);      //	    System.out.println("Server:  successfully sent "+s);    } catch (IOException e) {             System.out.println("Server:  socket error in send");      this.server.removeBabySitter(this);    }  }  /**    * Recieves new info from clients.   *   * @see Server#sendToAllExcept   */  public void run() {    System.out.println("Server:  BabySitter running");    while( !this.stopped )      {	try {	  String s = (String)ois.readObject();	  System.out.println( "Server:  just read '" + s +"' from " +			      this.sock.getInetAddress().getHostName() +			      ", port " + this.sock.getPort() );	  if( s == null || s == "" ) {	    this.server.removeBabySitter(this);	    return;	  }	  this.server.sendToAllExcept(s,this);			} catch (IOException e) {	  System.out.println("Server:  socket error in run ");	  this.server.removeBabySitter(this);	} catch (ClassNotFoundException cnfe) {	  System.out.println("Server:  could not find class String.");	  this.server.removeBabySitter(this);	}	      }  }  /**   * Should only be called by Server.   * User server.removeBabySittter( BabySitter ) instead.   *   * @see Server#removeBabySitter   */  protected void stop() {    System.out.println( "Server:  closing connection from " +			this.sock.getInetAddress().getHostName() +			" on port " + this.sock.getPort() );    try {      this.ois.close();    } catch (IOException e) {}        try {      this.oos.close();    } catch (IOException e) {}    try {      this.sock.close();    } catch (IOException e) {}    this.stopped = true;  }	    }/* * $Log: BabySitter.java,v $ * Revision 1.2  2003/09/23 15:18:08  gus * javadoc fix * * Revision 1.1.1.1  2002/06/05 21:56:32  root * CS101 comes to Olin finally. * * Revision 1.7  1998/07/24 17:13:36  tparnell * Placate new javadoc behavior * * Revision 1.6  1998/07/22 15:42:22  tparnell * moved to cs101.net * * Revision 1.5  1998/07/20 21:35:50  tparnell * Moved from Data*Stream to Object*Stream. * * Revision 1.4  1998/07/08 16:11:49  tparnell * update to JDK1.2: removed refrences to Thread.stop() since it has been * deprecated in 1.2 * * Revision 1.3  1998/06/24 21:24:27  tparnell * added logging to file * */

⌨️ 快捷键说明

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