📄 server.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.examples.simplesystem;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Vector;import net.sf.cscc.CommunicationEvent;import net.sf.cscc.CommunicationEventObserver;import net.sf.cscc.DataEvent;import net.sf.cscc.DataEventBaseObserver;import net.sf.cscc.DataEventReceivingObserver;import net.sf.cscc.ServerConnectionManager;/** * This class provides a server application, which demonstrates the usage * of this client/server component. The server application allows to connect * several clients on a on a specific port and send then some information about * the user over the net. The data is sent by calling the method * {@link net.sf.cscc.ServerConnectionManager#sendBroadCastEvent(DataEvent de)} * which sends the data event to all connected clients. To send a data * event to one specific client the method * {@link net.sf.cscc.ServerConnectionManager#sendDataEvent(DataEvent de, Object clientId)} * can be used. <br> * Important for using the client/server component is the implementation of the * methods {@link #receiveEvent(CommunicationEvent ce)} and {@link #receiveEvent(DataEvent de)} * which allows to receive data and communication events from the client/server * component. It allows to track all the data events and all the communication events. * * @author Silvio Meier * @copyright Silvio Meier, Tobias Reinhard, 2003 * @history 2003-05-15 SM First Version. * 2003-05-16 SM Comments and full implemented functionality. * 2003-05-17 SM Comments corrected * 2003-05-28 SM When pressing the disconnect button and * no selected item in the client list was selected * a null pointer exception was caused --> violating * a precondition, this bug is removed now.<br> * Additional linebreak added when displaying a received * message. * 2003-06-10 SM Additional output, the client identification number * is now also written to the output channel. * 2003-06-10 SM {@link net.sf.cscc.ServerConnectionManager#dispose()} method * is now used to release consumed resources. * 2004-12-13 SM Some cleaning of unused things. * 2006-11-28 SM Comments revised. * @version $Date: 2007/07/01 17:04:07 $, $Author: reode_orm $, $Revision: 1.1 $ */public class Server extends javax.swing.JFrame implements ActionListener, DataEventReceivingObserver, CommunicationEventObserver { /** * Serialization id. */ private static final long serialVersionUID = -5891230386648410135L; /** * Specifies the TCP/IP port number which is used to connect to the server. */ public static final int PORT_NUMBER = 2510; /** * Button for disconnecting all clients. */ private javax.swing.JButton disconnectAll; /** * List which shows the identification number of all clients. */ private javax.swing.JList clientList; /** * Left side panel, which contains information about connected clients. */ private javax.swing.JPanel panelLeft; /** * Message scroller used for the textarea used for displaing messages * from the clients and the communicaiton. */ private javax.swing.JScrollPane messagesScroller; /** * Button for disconnecting one client. */ private javax.swing.JButton disconnect; /** * Right panel contains information about */ private javax.swing.JPanel panelRightTop; /** * Textarea which is used to show all the messages which * occur. */ private javax.swing.JTextArea clientMessages; /** * Label which indicates the message box. */ private javax.swing.JLabel clientMessagesLabel; /** * Label indicating the list of clients. */ private javax.swing.JLabel clientListLabel; /** * Textfield for entering the port number. */ private javax.swing.JTextField port; /** * Contains data about listening for TCP/IP connections. */ private javax.swing.JPanel panelRightBottom; /** * Label which indicates the textfield for entering the port number. */ private javax.swing.JLabel portLabel; /** * Button for starting the listening process. */ private javax.swing.JButton beginListening; /** * The server connection manager which handles the communication with * the clients. */ private ServerConnectionManager serverConnectionManager; /** * This flag indicates if the server application is currently listening for * incoming connections. */ private boolean listening = false; /** * This vector provides a list of clients. The list of clients consists of * {@link java.lang.Integer} objects */ private Vector clients; /** * Constructor initializes the GUI of the server application and creates * a {@link net.sf.cscc.ServerConnectionManager} object for the communication * with the clients. * @pre true * @post true */ public Server() { clients = new Vector(); serverConnectionManager = new ServerConnectionManager(PORT_NUMBER); serverConnectionManager.addObserver((CommunicationEventObserver)this); serverConnectionManager.addObserver((DataEventReceivingObserver)this); initComponents(); } /** * This method initializes the GUI. * @pre true * @post true */ private void initComponents() { panelLeft = new javax.swing.JPanel(); clientList = new javax.swing.JList(); clientListLabel = new javax.swing.JLabel(); disconnect = new javax.swing.JButton(); disconnectAll = new javax.swing.JButton(); panelRightTop = new javax.swing.JPanel(); clientMessagesLabel = new javax.swing.JLabel(); messagesScroller = new javax.swing.JScrollPane(); clientMessages = new javax.swing.JTextArea(); panelRightBottom = new javax.swing.JPanel(); portLabel = new javax.swing.JLabel(); port = new javax.swing.JTextField(); beginListening = new javax.swing.JButton(); // action listener
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -