public class MessageSender implements Runnable
1: // Fig. 24.26: MessageSender.java
2: // Sends a message to the chat server in a separate runnable.
3: package com.deitel.messenger.sockets.client;
4:
5: import java.io.IOException;
6: import java.util.Formatter;
7: import java.net.Socket;
8:
9: import static com.deitel.messenger.sockets.SocketMessengerConstants.*;
10:
11: public class MessageSender implements Runnable
12: {
13: private Socket clientSocket; // Socket over which to send message
14: private String messageToSend; // message to send
15:
16: public MessageSender( Socket socket, String userName, String message )
17: {
18: clientSocket = socket; // store socket for client
19:
20: // build message to be sent
21: messageToSend = userName + MESSAGE_SEPARATOR + message;
22: } // end MessageSender constructor
23:
24: // send message and end
25: public void run()
26: {
27: try // send message and flush formatter
28: {
29: Formatter output =
30: new Formatter( clientSocket.getOutputStream() );
31: output.format( "%s\n", messageToSend ); // send message
32: output.flush(); // flush output
33: } // end try
34: catch ( IOException ioException )
35: {
36: ioException.printStackTrace();
37: } // end catch
38: } // end method run
39: } // end class MessageSender
40:
41:
42: /**************************************************************************
43: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
44: * Pearson Education, Inc. All Rights Reserved. *
45: * *
46: * DISCLAIMER: The authors and publisher of this book have used their *
47: * best efforts in preparing the book. These efforts include the *
48: * development, research, and testing of the theories and programs *
49: * to determine their effectiveness. The authors and publisher make *
50: * no warranty of any kind, expressed or implied, with regard to these *
51: * programs or to the documentation contained in these books. The authors *
52: * and publisher shall not be liable in any event for incidental or *
53: * consequential damages in connection with, or arising out of, the *
54: * furnishing, performance, or use of these programs. *
55: *************************************************************************/