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

📄 server.java

📁 java
💻 JAVA
字号:
/***************************************************
*  程序文件名称: Server.java
*  功能:多用户聊天室的服务器端程序
***************************************************/
import java.io.*; 
import java.net.*; 
import java.util.*; 

public class Server extends ServerSocket 
{ 
  private static final int SERVER_PORT = 10000; 
  Vector vector1 =new Vector(); 
  Vector vector2 =new Vector(); 

  public Server() throws IOException 
  { 
    super(SERVER_PORT); 
    try 
    { 
     while (true) 
     { 
       Socket socket = accept(); 
       new CreateServerThread(socket,vector1,vector2); 
     } 
    } 
    catch (IOException e)  {} 
    finally 
    { 
      close(); 
    } 
   } 
  public static void main(String[] args) throws IOException 
  { 
    new Server(); 
  } 
} 

//---建立服务器端线程类 CreateServerThread 
class CreateServerThread extends Thread 
{ 
 Socket client;   //存放客户端套接字socket;

 DataInputStream in; //线路输入信息;
 DataOutputStream out; //线路输出信息;

 Vector vector1;     //聊天室内客户的信息;
 Vector vector2;     //聊天室内客户的信息;
 public boolean bool=false; 
 String ss=null;  
 String string=null; 
 int iii; 
 String str=null; 
 Enumeration enu; //存放建立连接的客户向量对象;

 public CreateServerThread(Socket s,Vector vec1,Vector vec2) throws IOException 
 { 
  client = s; 
  vector1=vec1; 
  vector2=vec2; 

  out=new DataOutputStream(client.getOutputStream()); 
  in=new DataInputStream(client.getInputStream()); 

  start(); 
 } 

 public void run() 
 { 
  StringTokenizer st; //字符串分析器;
  StringTokenizer stc; //字符串分析器;
  try 
  { 
   while (true) 
   { 
    ss=in.readUTF(); 
	//判断接收的字符串前缀中是否包含有"新用户";
	//(1)如果包含有"新用户";
    if(ss.startsWith("新用户")){ 
      if(vector1.contains(ss) ){ 
          out.writeUTF("该用户名已注册"); 
       } 
      else{ 
        out.writeUTF("可以注册"); 
        str=ss; 
      vector1.add(ss);
         //获取向量vector1的枚举对象; 
		Enumeration enu=vector1.elements();
        //遍历当前散列表;
		while(enu.hasMoreElements()){ 
          out.writeUTF((String)enu.nextElement()); 
         } 
        bool=true; 
        //获取向量vector2的枚举对象;
		Enumeration enuc=vector2.elements(); 
        //遍历当前散列表;
		while(enuc.hasMoreElements()){ 
          CreateServerThread th=(CreateServerThread)enuc.nextElement(); 
          th.out.writeUTF(ss); 
         } 
        stc=new StringTokenizer(ss,":"); 
        string=stc.nextToken(); 
        string=stc.nextToken(); 
        Enumeration enuc1=vector2.elements(); 
        while(enuc1.hasMoreElements()){ 
          CreateServerThread th=(CreateServerThread)enuc1.nextElement(); 
          th.out.writeUTF(string+"...上线了"); 
         } 
        vector2.add(this); 
       } 
     }
     //(2)否则是已经注册正在聊天室内的客户 
    else if(ss.startsWith("下线了")||client.isClosed()){ 
      st=new StringTokenizer(ss,":"); 
      string=st.nextToken(); 
      string=st.nextToken(); 
      iii=vector1.indexOf(str); 
      vector1.remove(iii); 
      Enumeration enu=vector2.elements(); //获取向量vector2的枚举对象;
      while(enu.hasMoreElements()){ 
        CreateServerThread th=(CreateServerThread)enu.nextElement(); 
        if(th!=this&&th.isAlive()) 
          th.out.writeUTF("下线了:"+str.substring(str.indexOf(":")+1)); 
        th.out.writeUTF(str.substring(str.indexOf(":")+1)+"...下线了"); 
        } 
      vector2.remove(this); 
      break; 
    }   //  elseif  end
    // 是客户端发来的信息,给予转发出去。
    else{ 
      enu=vector2.elements(); 
      while(enu.hasMoreElements()){ 
        CreateServerThread th=(CreateServerThread)enu.nextElement(); 
        th.out.writeUTF(ss); 
       } 
    }   //  else end
   }   //while(true) end
  }   //try end
  catch (IOException e) 
  { 
   try{ 
     iii=vector1.indexOf(str);
     vector1.remove(iii); 
     Enumeration enun=vector2.elements(); 
     while(enun.hasMoreElements()){ 
       CreateServerThread th=(CreateServerThread)enun.nextElement(); 
       if(th!=this&&th.isAlive()) 
         th.out.writeUTF("下线了:"+str.substring(str.indexOf(":")+1)); 
       th.out.writeUTF(str.substring(str.indexOf(":")+1)+"...下线了"); 
      } 
    vector2.remove(this); 
    return; 
    }
   catch(Exception ee){return;} 
  } // catch end
 }  // run() end
}

⌨️ 快捷键说明

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