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

📄 chatwatcher.java

📁 java3D game engine design of the source [three-dimensionalvirtualrealitynetworkprogram] - "virtual
💻 JAVA
字号:

// ChatWatcher.java
// Andrew Davison, June 2003, dandrew@ratree.psu.ac.th

/* A threaded ChatWatcher object watches for messages 
   coming from the server.

   Message formats:
      * "WHO$$ cliAddr1 & port1 & ... cliAddrN & portN & "
           -- a response to a "who" message

      * "(cliAddr,port): msg"
           -- a broadcast message, originally from the client
              with the specified address and port number

   The messages are placed in the ChatClient's
   GUI text area by calling its showMsg() method.
*/

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;


public class ChatWatcher extends Thread
{
  private ChatClient client;   // ref to top-level client
  private BufferedReader in;   // stream coming from the server


  public ChatWatcher(ChatClient c, BufferedReader i)
  {  client = c; in = i;  }


  public void run()
  /* Read a server message, display it, repeat.
     A message can be:
			"WHO$$ n1 & p1 & .... nN & pN & "
     or     "(cliAddr,port): msg"

     "WHO$$" messages are reformatted in showWho(), but 
     other messages are displayed immediately by calling 
     showMsg() in the client.
  */
  { String line;
    try {
      while ((line = in.readLine()) != null) {
        if ((line.length() >= 6) &&     // "WHO$$ "
            (line.substring(0,5).equals("WHO$$")))
          showWho( line.substring(5).trim() );    
		    // remove WHO$$ keyword and surrounding space
        else  // show immediately
          client.showMsg(line + "\n");
      }
    }
    catch(Exception e)    // socket closure will cause termination of while
    { // System.out.println( e );  
      JOptionPane.showMessageDialog( null, 
           "Link to Server Lost!", "Connection Closed", 
			JOptionPane.ERROR_MESSAGE);
      System.exit( 0 );
    }
  } // end of run()


  private void showWho(String line)
  /*  line has the format:
             "cliAddr1 & port1 & ... cliAddrN & portN & "
      Reformat it before calling showMsg() in the client.
  */
  { StringTokenizer st = new StringTokenizer(line, "&");
    String addr;
    int port;
    int i = 1;
    try {
      while (st.hasMoreTokens()) {
        addr = st.nextToken().trim();
        port = Integer.parseInt( st.nextToken().trim() );
        client.showMsg("" + i + ". " + addr + " : " + port + "\n");
        i++;
      }
      // client.showMsg("\n");
    }
    catch(Exception e)
    { client.showMsg("Problem parsing who info.\n");
      System.out.println("Parsing error with who info: \n" + e);  
    }
  }  // end of showWho()


}  // end of ChatWatcher class

⌨️ 快捷键说明

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