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

📄 chatserver.java

📁 上传的聊天系统程序与QQ程序差不多,只是功能没有QQ那么强大
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 {
	    Client c=(Client)clients.elementAt(i);
		String s=c.name;
		if(s.equals(reciver)==true)
		{   
		   c.send(msg);
		   System.out.println(msg);
		}
	 }
  }
  
  public static synchronized void sendClients(Client s,String Truesay,String Falsesay)  
  {
  	 String Tsay=Truesay;
  	 String Fsay=Falsesay;
  	
     for(int i=0;i<clients.size();i++)
	 {
	    Client c=(Client)clients.elementAt(i);
		if (c.equals(s))
			c.send("MSG:ALL:"+" "+Tsay);
		else
			c.send("MSG:ALL:"+" "+Fsay);
     }
  }

  public static void closeAll()                             //实现关闭所有连接信息
  {
     while(clients.size()>0)                                 //遍历clients数组删除所有连接客户信息
	 {
	    Client c=(Client)clients.firstElement();
		try
		{
		   c.socket.close();
		}
		catch(IOException e)
	    {
		   System.out.println("Error:"+e);
		}
		finally
		{
		   clients.removeElement(c);
		}
	 }//end of while
  }//end of closeAll method

  public static boolean checkName(Client newclient)         //实现检查连接客户的socket信息是否合法
  {
     for(int i=0;i<clients.size();i++)
	 {
	     Client c=(Client)clients.elementAt(i);
		 if((c!=newclient)&&c.equals(newclient.name))
			 return false;
	 }
	 return(true);
  }//end of checkName method

  public static synchronized void disconnect(Client c)         //实现断开单个客户的方法
  {
     try
	 { 
		jList1.addItem(c.ip+"断开连接");                      //在服务器端程序的list框中显示断开信息		 
        int connum=--ChatServer.active_connects;                       //将连接数减1
        String constr="目前有"+connum+"客户相连";    //在状态栏里显示连接数
	    statusBar.setText(constr);
        c.send(new String("QUIT"));                           //向客户发送断开连接信息
	    c.socket.close(); 
	 }
	 catch(IOException e)
	 {
	    System.out.println("Error:"+e);
	 }
	 finally
	 {
		clients.removeElement(c);                       //从clients数组中删除此客户的相关socket等信息
	 }
  }
  
 public static synchronized void disconnect(Client c,String s)         //实现断开单个客户的方法
  {
     try
	 { 
        c.send(new String("QUIT"));                           //向客户发送断开连接信息
		c.socket.close(); 
	 }
	 catch(IOException e)
	 {
	    System.out.println("Error:"+e);
	 }
	 finally
	 {
		clients.removeElement(c);                       //从clients数组中删除此客户的相关socket等信息
	 }
  }
}
  
  class Client extends Thread                              //实现 Client线程类
  {
    Socket socket;
    String h;                                       //用来存储一个连接客户的socket信息
	String name;                                         //用来存储客户的连接姓名
	String ip;                                           //用来存储客户的ip信息
 	DataInputStream dis;                                 //用来实现接受从客户端发来的数据流
	DataOutputStream dos;                                      //用来实现向客户端发送信息的输出流

	public void send(String msg)                   //实现向客户端发送信息的方法
	{
       try
       {
	      dos.writeUTF(msg);                                  //用输出流发送信息
	   }
       catch(IOException e)
       {
          System.out.println("Error:"+e);
       }
	}

	public  Client(Socket s)                            //Client线程类的构造器
	{
	   socket=s;                                           
	   try
	   {
	       dis=new DataInputStream(socket.getInputStream());  //存储特定客户socket的输入流接受s这个客户发送到服务器端的信息
		   dos=new DataOutputStream(socket.getOutputStream());      //存储特定客户socket的输出流发送服务器给s这个客户的信息
		   String info;
           while(true)
           {
              info=dis.readUTF();                   //读取接受来的信息
		      if(info!=null) break;
           }
		   StringTokenizer stinfo=new StringTokenizer(info,":");  //用StringTokenizer类来读取用":"分段字符
		   String head=stinfo.nextToken();                        //head用来存储类似于关键字的头信息
           if(head.equals("APPLY"))                   //如果关键字是QUIT则是客户端发来断开连接的信息
		   {
		      try
		      {
		         String []shortstring=info.split(":");
		         ip=shortstring[4];
		         String msg=info;
		         applynum(msg);
		      }
		      catch (Exception e){}
		   }
		   if(head.equals("Confim"))                 
		   {
		      try
		      {
		         String msg=info;
		         confim(msg);
		         if(stinfo.hasMoreTokens())		   
			       name=stinfo.nextToken();                               //关键字后的第二段数据是客户名信息
			     if(stinfo.hasMoreTokens())
			       h=stinfo.nextToken();                                  //关键字后的第三段数据是客户ip信息
			     if(stinfo.hasMoreTokens())
			       ip=stinfo.nextToken(); 
			     System.out.println(name);
			     System.out.println(ip);
		      }
		      catch (Exception e){}
		   }
	   }
	   catch(IOException e)
	   {
	       System.out.println("Error:"+e);
	   }  
	}//end of Client constrator

	public void run()                                          //线程运行方法
	{
	   while(true)
	   {
	      String line=null;
		  try
		  {
		     line=dis.readUTF();
		     System.out.println(line);			//读取客户端发来的数据流
	      }
		  catch(IOException e)
		  {
		     System.out.println("Error"+e);
			 ChatServer.disconnect(this);
			 ChatServer.notifyRoom();
			 return;
		  }
		  if(line==null)    //客户已离开
	      {
			 ChatServer.disconnect(this);
			 ChatServer.notifyRoom();
			 return;
		  }
		  StringTokenizer st=new StringTokenizer(line,":");
		  String keyword=st.nextToken();
		  if(keyword.equals("MSG"))                          //如果关键字是MSG则是客户端发来的聊天信息
		  {
		      String reciver=st.nextToken();
		      String msg=new String("FRIEND:");     //在服务器端再重新建立一个字符串
			  msg=msg+name;
			  msg=msg+st.nextToken("\0");
			  System.out.println(msg);
			  ChatServer.sendClients(reciver,msg);                   //再将某个客户发来的聊天信息发送到每个连接客户的聊天栏中
		  }
		  else if(keyword.equals("QUIT"))                   //如果关键字是QUIT则是客户端发来断开连接的信息
		  {
		      ChatServer.disconnect(this);                  //服务器断开与这个客户的连接
			  ChatServer.notifyRoom();                      //继续监听聊天室并刷新其他客户的聊天人名list
			  this.stop(); 
			  return;                                  
		  }
		  else if(keyword.equals("INFO"))                 
		  {
		      try
		      {
		          String msg=line;
		          confim(msg);
		      }
		      catch(Exception e){}
		  }  
	   }
	}
	 public  void applynum(String message) throws Exception
    {
	  	String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=G://大三下学期//java//聊天//PP2007.mdb";
	
	    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

	    Connection con = DriverManager.getConnection(url,"","");

	    Statement st = con.createStatement();
	    ResultSet rs = st.executeQuery("select * from userlist");
	    ResultSetMetaData rsmd;
	    rsmd=rs.getMetaData();
	    int numCols=rsmd.getColumnCount();
	    int numline=1;
	    while(rs.next())
	    {
	       numline++;
	    }
	    rs.close();
	    st.close();
	     
	    String numLine=String.valueOf(numline);
	    String[] shortString=message.split(":");
     
		String petname=shortString[1];
		String password=shortString[2];		   
		String sign=shortString[3];
	     
	    String sql="insert into userlist values (?,?,?,?)";
	    PreparedStatement pstmt=con.prepareStatement(sql);
	     
	    pstmt.setString(1,numLine);
	    pstmt.setString(2,petname);
	    pstmt.setString(3,password);
	    pstmt.setString(4,sign);
	     
	    this.send("申请成功,您的PP帐号为"+numLine);
	    pstmt.executeUpdate();
	    pstmt.close();
	    con.close();
	    this.socket.close();
    }
       
    public  void confim(String message) throws Exception
    {
	  	String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=G://大三下学期//java//聊天//PP2007.mdb";
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		Connection con = DriverManager.getConnection(url,"","");
	
	    Statement st = con.createStatement();
	    ResultSet rs = st.executeQuery("select * from userlist");
	     
	    String[] shortString=message.split(":");
	    String ppnum=shortString[1];
		String password=shortString[2];
		String cnum,cpw;
		System.out.println(message);
	    while(rs.next())
	    {
	     	cnum=rs.getString(1);
	     	cpw=rs.getString(3);
	     	if(ppnum.equals(cnum)&&password.equals(cpw))
	     	{
	     		dos.writeUTF("true:true");
	     		rs.close();
	            st.close();
	            con.close();
	            return;
	     	}
	     	else if(ppnum.equals(cnum)&&!password.equals(cpw))
	     	{
	     		dos.writeUTF("true:false");
	     		rs.close();
			    st.close();
			    con.close();
			    this.socket.close();
			    return;
	     	}
	    }
	     
	    if(!rs.next())
	        dos.writeUTF("false:false");   
	    rs.close();
	    st.close();
	    con.close();
	    this.socket.close();
    }

}  //end of class Client


⌨️ 快捷键说明

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