All pastes #753140 Raw Edit

Stuff

public java v1 · immutable
#753140 ·published 2007-10-29 00:00 UTC
rendered paste body
import java.awt.event.*;import java.util.*;import java.awt.*;import java.io.*;import java.net.*;/** * * CommServer * <BR><BR> * Generic Flash Communication Server.  All communications sent and received * are (must be) terminated with a null character ('\0') consistent with * the way Flash does socket communications.  Any client which uses this * protocol should also work. * <BR><BR> * The server accepts messages from connected clients and broadcasts (verbatim) * those messages to all connected clients.  When clients connect or disconnect * the server also broadcasts the number of clients via a NUMCLIENTS tag. * * Usage: java CommServer [port] * * @author  Derek Clayton   derek_clayton@iceinc.com * @version 1.0.1 */public class CommServer {    private Vector clients = new Vector();  // a list of all connected clients    ServerSocket server;                    // the server        /**     * Constructor for the CommServer.  Begins the start server process.     * @param   port   Port number the server should listen to for connections.    */    public CommServer(int port) {        startServer(port);    }    /**     * Starts the server and listens for connections.     * @param   port   Port number the server should listen to for connections.    */    private void startServer(int port) {        writeActivity("Attempting to Start Server");                try {            // --- create a new server             server = new ServerSocket(port);            writeActivity("Server Started on Port: " + port);            // --- while the server is active...            while(true) {                // --- ...listen for new client connections                Socket socket = server.accept();                CSClient client = new CSClient(this, socket);                // --- writeActivity(client.getIP() + " connected to the server.");				writeActivity("This should send the client IP:" + client.getIP());				broadcastMessage("<NUMCLIENTS>" + client.getIP()                               + "</NUMCLIENTS>");                // --- add the new client to our client list                 clients.addElement(client);                // --- start the client thread                client.start();		                // --- broadcast the new number of clients			                // ---broadcastMessage("<NUMCLIENTS>" + clients.size()                // ---               + "</NUMCLIENTS>");            }        } catch(IOException ioe) {            writeActivity("Server Error...Stopping Server");            // kill this server            killServer();        }     }    /**     * Broadcasts a message too all connected clients.  Messages are terminated     * with a null character.     * @param   message    The message to broadcast.    */    public synchronized void broadcastMessage(String message) {        // --- add the null character to the message        message += '\0';                // --- enumerate through the clients and send each the message        Enumeration enumer = clients.elements();        while (enumer.hasMoreElements()) {            CSClient client = (CSClient)enumer.nextElement();			if(true){            client.send(message);			}			writeActivity("<Message sent to Client " + CSClient client);        }    }    /**     * Removes clients from the client list.     * @param   client    The CSClient to remove.    */    public void removeClient(CSClient client) {        writeActivity("Arte! ... the client at IP:" + client.getIP() + " has left the server! D:");        broadcastMessage("<NUMCLIENTS>" + client.getIP() + "</NUMCLIENTS>");        // --- remove the client from the list        clients.removeElement(client);                // --- broadcast the new number of clients        broadcastMessage("<NUMCLIENTS>" + clients.size() + "</NUMCLIENTS>");    }    /**     * Writes a message to System.out.println in the format     * [mm/dd/yy hh:mm:ss] message.     * @param   activity    The message.    */    public void writeActivity(String activity) {        // --- get the current date and time        Calendar cal = Calendar.getInstance();        activity = "[" + cal.get(Calendar.MONTH)                  + "/" + cal.get(Calendar.DAY_OF_MONTH)                  + "/" + cal.get(Calendar.YEAR)                  + " "                  + cal.get(Calendar.HOUR_OF_DAY)                  + ":" + cal.get(Calendar.MINUTE)                  + ":" + cal.get(Calendar.SECOND)                  + "] " + activity + "\n";        // --- display the activity        System.out.print(activity);    }    /**     * Stops the server.    */    private void killServer() {        try {            // --- stop the server            server.close();            writeActivity("Server Stopped");        } catch (IOException ioe) {            writeActivity("Error while stopping Server");        }    }        public static void main(String args[]) {        CommServer myCS = new CommServer(1338);    }}