objectreceiver.java

来自「自己写的一个聊天的小程序 请多多指教」· Java 代码 · 共 165 行

JAVA
165
字号
/* * Copyright (c) 2003 - 2007, Silvio Meier and Tobias Reinhard * * All rights reserved. *  * Redistribution and use in source and binary forms,  * with or without modification, are permitted provided  * that the following conditions are met: * *   o Redistributions of source code must retain the above  *     copyright notice, this list of conditions and the  *     following disclaimer. *   o Redistributions in binary form must reproduce the above  *     copyright notice, this list of conditions and the  *     following disclaimer in the documentation and/or other  *     materials provided with the distribution. *   o The names of its contributors may not be used to endorse  *     or promote products derived from this software without  *     specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package net.sf.cscc;import java.io.IOException;import java.io.ObjectInputStream;import java.net.Socket;/** *   This class implements a threading routine listening *   for incoming data events. If data events are received, they *   are enqueued in the incoming event queue.  * *   @author            Silvio Meier (SM) *   @copyright      	Silvio Meier, Tobias Reinhard, 2003 *   @history			2003-05-09 SM First Version *                      2003-05-14 SM working on additional methods and events, *                      correction of some comments. *                      2003-05-20 TR Comments corrected *                      2003-06-24 SM Exception handling extended. *  					2004-12-13 SM Some cleaning of unused things. *  					2006-11-28 SM Comments revised. *   @version		$Date: 2007/07/01 17:04:05 $, $Author: reode_orm $, $Revision: 1.1 $ * */class ObjectReceiver extends Thread {       /**     *  The owning communication controller implements a callback interface      *  for the sake of decoupling.     */    private CommunicationController controller;        /**     * Reference to the socket of the connection, used     * for the communication between server and client.     */    private Socket socket = null;        /**     *  Object input stream deserializes the messages received over the network.     */    private ObjectInputStream in = null;        /**     *	If true, the connection is open. If the connection is     *  not open, the corresponding thread is not executing the {@link #run()}     *  method.     */    private boolean connected;        /**     *   Intializes the ObjectReceiver.     *        *   @pre s != null && controller != null     *   @post connected == false     *   @param s Socket object representing the connection between client     *          and server.     *   @param controller Controller of the communication.     *   @throws net.sf.cscc.CsccException if the receiver thread could      *           not be started correctly.     */    ObjectReceiver(Socket s, CommunicationController controller) {        // indicates if connection is active        connected = false;                       // callback interface to the owner         this.controller = controller;        this.setPriority(Thread.MAX_PRIORITY);        // socket        this.socket = s;        try{            // creates new input and output stream for the client            in = new ObjectInputStream(socket.getInputStream());        }        catch (Exception e) {            throw new CsccException(e.getMessage());        }        connected = true;        start();    }        /**     *   This method closes the receiving connection to a client.      *   After calling the {@link #close()} method, this object can not be used      *   any more.     *     *   @pre true     *   @post connected == false     *   @throws net.sf.cscc.CsccException if receiver thread could not      *   		 be closed correctly.      */    public  void close() {        if (connected) {            connected = false;            try {                // close object input stream                           in.close();            }            catch (IOException e) {                throw new CsccException(e.getMessage());            }        }    }        /**     *   This method runs the receiving thread of the communication between      *   server and client. Every data object which is received from the      *   communication partner is interpreted as object, i.e. incoming data      *   tried to be deserialized as Java object.     *        *   @pre true     *   @post connected == false     */    public  void run() {        connected = true;        // run until the client connection dies.        try {            while (connected) {                            // look if there is incoming data in the connection                // and if there is, read an object                Object o = in.readObject();                                controller.enqueueIncomingDataEvent((DataEvent)o);                               }        } catch (Exception ex) {   // throws no exception                                             if (connected) {                    connected = false;                        controller.communicationBroken(this);                }                            }    }    }

⌨️ 快捷键说明

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