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

📄 chatserverhandler.java

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

// ChatServerHandler.java
// Andrew Davison, June 2003 (dandrew@ratree.psu.ac.th)
/*
   A threaded ChatServerHandler deals with a client.

   Details about a client are maintained in a ChatGroup 
   object, which is referenced by all the threads.

   The ChatGroup object handles message broadcasting
   via its broadcast() method.

   Possible client messages:
		who				-- a list of users is returned
        bye             -- client is disconnecting
		any text		-- which is broadcast with
						   (cliAddr,port): at its front 
*/

import java.net.*;
import java.io.*;


public class ChatServerHandler extends Thread
{
  private Socket clientSock;    // client details
  private String cliAddr;
  private int port;

  private ChatGroup cg;    // shared by all threads


  public ChatServerHandler(Socket s, ChatGroup cg)
  {
    this.cg = cg;
    clientSock = s;
    cliAddr = clientSock.getInetAddress().getHostAddress();
    port = clientSock.getPort();
    System.out.println("Client connection from (" + 
						cliAddr + ", " + port + ")");
  } // end of ChatServerHandler()


  public void run()
  {
    try {
      // Get I/O streams from the socket
      BufferedReader in  = new BufferedReader( 
	  		new InputStreamReader( clientSock.getInputStream() ) );
      PrintWriter out = 
			new PrintWriter( clientSock.getOutputStream(), true );  // autoflush

      cg.addPerson(cliAddr, port, out);  // add client details to ChatGroup

      processClient(in, out);            // interact with client
 
      // the client has finished when execution reaches here
      cg.delPerson(cliAddr, port);       // remove client details
      clientSock.close();
      System.out.println("Client (" + cliAddr + ", " + 
								port + ") connection closed\n");
    }
    catch(Exception e)
    {  System.out.println(e);  }
  }  // end of run()


   private void processClient(BufferedReader in, PrintWriter out)
   /* Stop when the input stream closes (is null) or "bye" is sent
      Otherwise pass the input to doRequest(). */
   {
     String line;
     boolean done = false;
     try {
       while (!done) {
         if((line = in.readLine()) == null)
           done = true;
         else {
           System.out.println("Client (" + cliAddr + ", " + 
								port + "): " + line);
           if (line.trim().equals("bye"))
             done = true;
           else 
             doRequest(line, out);
         }
       }
     }
     catch(IOException e)
     {  System.out.println(e);  }
   }  // end of processClient()


  private void doRequest(String line, PrintWriter out)
  /*  The input line (client message) can be :
			who				-- a list of users is returned
      or	any text		-- which is broadcast with
							   (cliAddr,port) at its front 
  */
  { if (line.trim().toLowerCase().equals("who")) {
      System.out.println("Processing 'who'");
      out.println( cg.who() );
    }
    else  // use ChatGroup object to broadcast the message
      cg.broadcast( "("+cliAddr+", "+port+"): " + line);
  }  // end of doRequest()


}  // end of ChatServerHandler class

⌨️ 快捷键说明

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