⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 receivingthread.java

📁 这是java的一本书上的源代码
💻 JAVA
字号:
// ReceivingThread.java
// ReceivingThread is a Thread that listens for messages from a 
// particular client and delivers messages to a MessageListener.
package com.deitel.messenger.sockets.server;

import java.io.*;
import java.net.*;
import java.util.StringTokenizer;

import com.deitel.messenger.*;
import com.deitel.messenger.sockets.*;

public class ReceivingThread extends Thread {

   private BufferedReader input;
   private MessageListener messageListener;
   private boolean keepListening = true;
   
   // ReceivingThread constructor
   public ReceivingThread( MessageListener listener, Socket clientSocket ) 
   {
      // invoke superclass constructor to name Thread
      super( "ReceivingThread: " + clientSocket );
      
      // set listener to which new messages should be sent
      messageListener = listener;
      
      // set timeout for reading from clientSocket and create
      // BufferedReader for reading incoming messages
      try {         
         clientSocket.setSoTimeout( 5000 );
         
         input = new BufferedReader( new InputStreamReader( 
            clientSocket.getInputStream() ) );
      }
      
      // handle exception creating BufferedReader
      catch ( IOException ioException ) {
         ioException.printStackTrace();
      }
   
   } // end ReceivingThread constructor
   
   // listen for new messages and deliver them to MessageListener
   public void run() 
   {    
      String message;
      
      // listen for messages until stopped
      while ( keepListening ) {   

         // read message from BufferedReader
         try {            
            message = input.readLine();
         }
         
         // handle exception if read times out
         catch ( InterruptedIOException interruptedIOException ) {

            // continue to next iteration to keep listening
            continue;
         }
         
         // handle exception reading message
         catch ( IOException ioException ) {
            ioException.printStackTrace();            
            break;
         }

         // ensure non-null message
         if ( message != null ) {

            // tokenize message to retrieve user name and message body
            StringTokenizer tokenizer = new StringTokenizer( 
               message, SocketMessengerConstants.MESSAGE_SEPARATOR );

            // ignore messages that do not contain a user
            // name and message body               
            if ( tokenizer.countTokens() == 2 ) 

               // send message to MessageListener
               messageListener.messageReceived( 
                  tokenizer.nextToken(),   // user name
                  tokenizer.nextToken() ); // message body

            else

               // if disconnect message received, stop listening
               if ( message.equalsIgnoreCase( 
                  SocketMessengerConstants.MESSAGE_SEPARATOR +
                  SocketMessengerConstants.DISCONNECT_STRING ) ) 
                  stopListening();

         } // end if

      } // end while  
      
      // close BufferedReader (also closes Socket)
      try {         
         input.close();      
      }
      
      // handle exception closing BufferedReader
      catch ( IOException ioException ) {
         ioException.printStackTrace();     
      }       
 
   } // end method run
   
   // stop listening for incoming messages
   public void stopListening() 
   {
      keepListening = false;
   }

} // end class ReceivingThread


/**************************************************************************
 * (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.                     *
 *************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -