public class PacketReceiver implements Runnable
1: // Fig. 24.27: PacketReceiver.java
2: // PacketReceiver listens for DatagramPackets containing
3: // messages from a DeitelMessengerServer.
4: package com.deitel.messenger.sockets.client;
5:
6: import java.io.IOException;
7: import java.net.InetAddress;
8: import java.net.MulticastSocket;
9: import java.net.DatagramPacket;
10: import java.net.SocketTimeoutException;
11: import java.util.StringTokenizer;
12:
13: import com.deitel.messenger.MessageListener;
14: import static com.deitel.messenger.sockets.SocketMessengerConstants.*;
15:
16: public class PacketReceiver implements Runnable
17: {
18: private MessageListener messageListener; // receives messages
19: private MulticastSocket multicastSocket; // receive broadcast messages
20: private InetAddress multicastGroup; // InetAddress of multicast group
21: private boolean keepListening = true; // terminates PacketReceiver
22:
23: public PacketReceiver( MessageListener listener )
24: {
25: messageListener = listener; // set MessageListener
26:
27: try // connect MulticastSocket to multicast address and port
28: {
29: // create new MulticastSocket
30: multicastSocket = new MulticastSocket(
31: MULTICAST_LISTENING_PORT );
32:
33: // use InetAddress to get multicast group
34: multicastGroup = InetAddress.getByName( MULTICAST_ADDRESS );
35:
36: // join multicast group to receive messages
37: multicastSocket.joinGroup( multicastGroup );
38:
39: // set 5 second timeout when waiting for new packets
40: multicastSocket.setSoTimeout( 5000 );
41: } // end try
42: catch ( IOException ioException )
43: {
44: ioException.printStackTrace();
45: } // end catch
46: } // end PacketReceiver constructor
47:
48: // listen for messages from multicast group
49: public void run()
50: {
51: // listen for messages until stopped
52: while ( keepListening )
53: {
54: // create buffer for incoming message
55: byte[] buffer = new byte[ MESSAGE_SIZE ];
56:
57: // create DatagramPacket for incoming message
58: DatagramPacket packet = new DatagramPacket( buffer,
59: MESSAGE_SIZE );
60:
61: try // receive new DatagramPacket (blocking call)
62: {
63: multicastSocket.receive( packet );
64: } // end try
65: catch ( SocketTimeoutException socketTimeoutException )
66: {
67: continue; // continue to next iteration to keep listening
68: } // end catch
69: catch ( IOException ioException )
70: {
71: ioException.printStackTrace();
72: break;
73: } // end catch
74:
75: // put message data in a String
76: String message = new String( packet.getData() );
77:
78: message = message.trim(); // trim whitespace from message
79:
80: // tokenize message to retrieve user name and message body
81: StringTokenizer tokenizer = new StringTokenizer(
82: message, MESSAGE_SEPARATOR );
83:
84: // ignore messages that do not contain a user
85: // name and message body
86: if ( tokenizer.countTokens() == 2 )
87: {
88: // send message to MessageListener
89: messageListener.messageReceived(
90: tokenizer.nextToken(), // user name
91: tokenizer.nextToken() ); // message body
92: } // end if
93: } // end while
94:
95: try
96: {
97: multicastSocket.leaveGroup( multicastGroup ); // leave group
98: multicastSocket.close(); // close MulticastSocket
99: } // end try
100: catch ( IOException ioException )
101: {
102: ioException.printStackTrace();
103: } // end catch
104: } // end method run
105:
106: // stop listening for new messages
107: public void stopListening()
108: {
109: keepListening = false;
110: } // end method stopListening
111: } // end class PacketReceiver
112:
113:
114: /**************************************************************************
115: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
116: * Pearson Education, Inc. All Rights Reserved. *
117: * *
118: * DISCLAIMER: The authors and publisher of this book have used their *
119: * best efforts in preparing the book. These efforts include the *
120: * development, research, and testing of the theories and programs *
121: * to determine their effectiveness. The authors and publisher make *
122: * no warranty of any kind, expressed or implied, with regard to these *
123: * programs or to the documentation contained in these books. The authors *
124: * and publisher shall not be liable in any event for incidental or *
125: * consequential damages in connection with, or arising out of, the *
126: * furnishing, performance, or use of these programs. *
127: *************************************************************************/