// DeitelMessengerServer.java // DeitelMessengerServer is a multi-threaded, socket- and // packet-based chat server. package com.deitel.messenger.sockets.server; import java.util.*; import java.net.*; import java.io.*; import com.deitel.messenger.*; import com.deitel.messenger.sockets.*; public class DeitelMessengerServer implements MessageListener { // start chat server public void startServer() { // create server and manage new clients try { // create ServerSocket for incoming connections ServerSocket serverSocket = new ServerSocket( SocketMessengerConstants.SERVER_PORT, 100 ); System.out.println( "Server listening on port " + SocketMessengerConstants.SERVER_PORT + " ..." ); // listen for clients constantly while ( true ) { // accept new client connection Socket clientSocket = serverSocket.accept(); // create new ReceivingThread for receiving // messages from client new ReceivingThread( this, clientSocket ).start(); // print connection information System.out.println( "Connection received from: " + clientSocket.getInetAddress() ); } // end while } // end try // handle exception creating server and connecting clients catch ( IOException ioException ) { ioException.printStackTrace(); } } // end method startServer // when new message is received, broadcast message to clients public void messageReceived( String from, String message ) { // create String containing entire message String completeMessage = from + SocketMessengerConstants.MESSAGE_SEPARATOR + message; // create and start MulticastSendingThread to broadcast // new messages to all clients new MulticastSendingThread( completeMessage.getBytes() ).start(); } public static void main ( String args[] ) { new DeitelMessengerServer().startServer(); } } // end class DeitelMessengerServer /************************************************************************** * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and * * Prentice Hall. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/