public class DeitelMessengerServer implements MessageListener
1: // Fig. 24.18: DeitelMessengerServer.java
2: // DeitelMessengerServer is a multi-threaded, socket- and
3: // packet-based chat server.
4: package com.deitel.messenger.sockets.server;
5:
6: import java.net.ServerSocket;
7: import java.net.Socket;
8: import java.io.IOException;
9: import java.util.concurrent.Executors;
10: import java.util.concurrent.ExecutorService;
11:
12: import com.deitel.messenger.MessageListener;
13: import static com.deitel.messenger.sockets.SocketMessengerConstants.*;
14:
15: public class DeitelMessengerServer implements MessageListener
16: {
17: private ExecutorService serverExecutor; // executor for server
18:
19: // start chat server
20: public void startServer()
21: {
22: // create executor for server runnables
23: serverExecutor = Executors.newCachedThreadPool();
24:
25: try // create server and manage new clients
26: {
27: // create ServerSocket for incoming connections
28: ServerSocket serverSocket =
29: new ServerSocket( SERVER_PORT, 100 );
30:
31: System.out.printf( "%s%d%s", "Server listening on port ",
32: SERVER_PORT, " ..." );
33:
34: // listen for clients constantly
35: while ( true )
36: {
37: // accept new client connection
38: Socket clientSocket = serverSocket.accept();
39:
40: // create MessageReceiver for receiving messages from client
41: serverExecutor.execute(
42: new MessageReceiver( this, clientSocket ) );
43:
44: // print connection information
45: System.out.println( "Connection received from: " +
46: clientSocket.getInetAddress() );
47: } // end while
48: } // end try
49: catch ( IOException ioException )
50: {
51: ioException.printStackTrace();
52: } // end catch
53: } // end method startServer
54:
55: // when new message is received, broadcast message to clients
56: public void messageReceived( String from, String message )
57: {
58: // create String containing entire message
59: String completeMessage = from + MESSAGE_SEPARATOR + message;
60:
61: // create and start MulticastSender to broadcast messages
62: serverExecutor.execute(
63: new MulticastSender( completeMessage.getBytes() ) );
64: } // end method messageReceived
65: } // end class DeitelMessengerServer
66:
67:
68: /**************************************************************************
69: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
70: * Pearson Education, Inc. All Rights Reserved. *
71: * *
72: * DISCLAIMER: The authors and publisher of this book have used their *
73: * best efforts in preparing the book. These efforts include the *
74: * development, research, and testing of the theories and programs *
75: * to determine their effectiveness. The authors and publisher make *
76: * no warranty of any kind, expressed or implied, with regard to these *
77: * programs or to the documentation contained in these books. The authors *
78: * and publisher shall not be liable in any event for incidental or *
79: * consequential damages in connection with, or arising out of, the *
80: * furnishing, performance, or use of these programs. *
81: *************************************************************************/