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

📄 clientconnection.java

📁 自己写的一个聊天的小程序 请多多指教
💻 JAVA
字号:
/* * 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.net.Socket;/** *  This class represents the connection from a client to the server. It *  is instantiated by the {@link net.sf.cscc.ConnectionPortListener} *  as soon as particular client tries to connect to the server. * *	@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-16 SM Bug closing connection --> null pointer was *                     removed. *                     2003-05-17 SM Class is now able to assign a incoming *                     data event to a client (using a client identification object). *                     2003-05-20 TR Comments corrected *                     2003-06-24 SM Some corrections *                     2006-10-25 SM Comments revised *	@version          $Date: 2007/07/01 17:04:06 $, $Author: reode_orm $, $Revision: 1.1 $ */class ClientConnection implements CommunicationController {    /**     *  Represents the identification object which identifies the client     *  uniquely.     */    private Object clientId;    /**     * Thread which is responsible for sending objects.     */    private ObjectSender sender = null;    /**     *  Thread which is responsible for receiving objects.     */    private ObjectReceiver receiver = null;    /**     * Reference to the socket of the connection, used     * for the communication between server and client.     */    private Socket socket = null;    /**     *  Reference to the connection registry which is used to manage     *  each client that is connected to the server.     */    private ClientConnectionRegistry connectionRegistry;     /**     *  Status of this connection (true if the connection is open).     */    private boolean connected;    /**     *   Creates this ClientConnection, using the given {@link ClientConnectionRegistry}.     *   @pre clientId != null && s != null && registry != null     *   @post connected == false     *   @param s Socket object representing the connection between client     *          and server.     *   @param registry The client connection registry that will be used     *          to register the client represented by this ClientCconnection.     */    public ClientConnection(Object clientId, Socket s, ClientConnectionRegistry registry) {        sender = new ObjectSender(s, this);        receiver = new ObjectReceiver(s, this);        connected = true;        connectionRegistry = registry;        this.clientId = clientId;    }    /**     *   This method closes the connection to a client. A     *   ClientConnection object on which this method was called can not be     *   used any more. The connection and its represented client respectively     *   is deleted from the {@link net.sf.cscc.ClientConnectionRegistry}     *   object.     *     *   @pre true     *   @post connected == false     */    public  void close() {        if (connected) {            try {                connected = false;                if (sender != null) sender.close();                if (receiver != null) receiver.close();                if (socket != null) socket.close();                sender = null;                receiver = null;                socket = null;                connectionRegistry.notifyCommunicationEventObservers(                    new CommunicationEvent(CommunicationEvent.CONNECTION_CLOSED, clientId));                // remove connection from registry                connectionRegistry.unregisterConnection(clientId);            } catch (IOException ex)  {                connectionRegistry.unregisterConnection(clientId);            }        }    }    /**     *   This method sets the client ID. The client ID can be any object and     *    must identify the client uniquely.     *   @pre id != null     *   @post clientID == id     *   @param id The client ID which should be set for this object     */    public void setClientID(Object id) {        clientId = id;    }    /**     *   This method gets the client ID. The client ID can be any object     *   and identifies the client uniquely.     *   @pre true     *   @post return = clientID     *     *   @return Returns the currently set client ID of this ClientConnection     *           object.     */    public Object getClientID() {        return clientId;    }    /**     *   Enqueues a new DataEvent object at the end of the out-going     *   event queue.     *   @pre de != null     *   @post true     *   @param de DataEvent object which is enqued at the end of the queue.     */    public void enqueueDataEvent(DataEvent de) {        sender.enqueueDataEvent(de);    }    /**     *   This method is the implementation of the     *   {@link net.sf.cscc.CommunicationController}     *   interface. It handles the breaking of a connection.     *   @pre eventSender != null     *   @post true     *   @param eventSender The object from where the communication     *          broken event is coming from.     */    public void communicationBroken(Object eventSender) {        try {            if (eventSender == sender) {                receiver.close();                socket.close();            }            if (eventSender == receiver) {                sender.close();                socket.close();            }        } catch (Exception ex) {            // do nothing        }        sender = null;        receiver = null;        socket = null;        connectionRegistry.notifyCommunicationEventObservers(            new CommunicationEvent(CommunicationEvent.CONNECTION_BROKEN, clientId));        // remove connection from registry        connectionRegistry.unregisterConnection(clientId);        connected = false;    }    /**     *   This method is the implementation of the     *   {@link net.sf.cscc.CommunicationController}     *   interface. It handles incoming events which are     *   enqueued in the incoming event  queue.     *   @pre de != null     *   @post true     */    public void enqueueIncomingDataEvent(DataEvent de) {        de.setClientId(clientId);        connectionRegistry.enqueueIncomingDataEvent(de);    }}

⌨️ 快捷键说明

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