简单的java聊天室,出了点问题,欢迎帮忙,哈哈

来源:互联网 发布:禁止windows更新驱动 编辑:程序博客网 时间:2024/05/11 19:14

 package muyizhou.chat;
import java.io.*;
import java.net.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Event;
import java.util.*;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.*;
public class CServer extends JFrame{
 
 
 //保存当前处理客户端请求的处理器线程
 static Vector clientProcessors=new Vector(10);
 //当前的连接数
 static int activeConnects=0;
 
 
 
 //构造方法
 public CServer(){
  
  
 }
 

 
 //主方法
 public static void main(String args[]){
  
  CServer chatServer1=new CServer();
  chatServer1.setVisible(true);
  System.out.println("server starting........");
  Socket socket=null;
  ServerSocket server=null;
   try{
    //服务器端开始侦听
    server=new ServerSocket(5000);
   }catch(IOException e){
    System.out.println("Error"+e);
    System.exit(1);
   }
  
  
  
  while(true){
   try{
    
    
    socket=server.accept();
    
    if(socket!=null)
       {
        System.out.println(socket+"连接");
        }
    }catch(IOException e){
      System.out.println("Error"+e);
      }
                ClientProcessor c=new ClientProcessor(socket);
                c.start();
                //sendMsgToClients(msg);
   }
  }
   //关闭所有连接
 public static void closeAll(){
  while(clientProcessors.size()>0){
    ClientProcessor c=(ClientProcessor) clientProcessors.firstElement();
    try{
     //关闭socket连接和处理线程
     c.socket.close();
     c.toStop();
    }catch(IOException e){
     System.out.println("Error:"+e);
    }finally{
     clientProcessors.removeElement(c);
    }
   }
 }
 
 //向所有用户群发消息
 public static synchronized void sendMsgToClients(String msg){
  //msg=null;
 
   for(int i=0;i<clientProcessors.size();i++){
    ClientProcessor c=(ClientProcessor) clientProcessors.elementAt(i);
    System.out.println("send msg"+msg);
    c.send(msg);
    }
  }
  
  
 public static void disConnect(ClientProcessor client){
   disConnect(client,true);
  }
 public static synchronized void disConnect(ClientProcessor client,
      boolean toRemoveFromList){
    try{
    
     client.socket.close();
    }catch(IOException e){
     System.out.println("Error"+e);
     
    }
    }
   
  }


  
 
 
 

 

//处理客户端发送的请求的线程  
   
 class ClientProcessor extends Thread{
 Socket socket;
 BufferedReader br;
 PrintWriter pw;
 boolean running=true;
 String usr=null;
 

 
 public void send(String msg){
  usr=msg;
  try{
    usr=br.readLine();
    
   }catch(IOException e){
    System.out.println("Error:"+e);
       CServer.disConnect(this);
   } 
  pw.println(msg);
  
 }
 //构造方法
 public ClientProcessor(Socket s){
  socket=s;
  try{
   br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
   pw=new PrintWriter(socket.getOutputStream());
   //usr=br.readLine();
  
  }catch(IOException e){
   System.out.println("Error:"+e);
  }
 }
 

 public void run(){
  while(running){
   String line=null;
   try{
    line=br.readLine();
    System.out.println(line);
   }catch(IOException e){
    System.out.println("Error:"+e);
       CServer.disConnect(this);
   }
  CServer.sendMsgToClients(line); 
   
 }

     public void toStop(){
      running=false;
     }
    
   
    
}

 

客户端
package book.net.chat;
import java.io.*;
import java.net.Socket;
import java.net.InetAddress;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.StringTokenizer;
import javax.swing.border.TitledBorder;
public class CClient extends JFrame implements ActionListener{
 JLabel nameLabel=new JLabel();
 JLabel ipLabel=new JLabel();
 JTextField nameTextField=new JTextField(15);
 JTextField ipTextField=new JTextField(15);
 JTextField msgTextField=new JTextField(30);
 
 JButton connectButton=new JButton();
 JButton disconnectButton=new JButton();
 
 JTextArea chatContentTextArea=new JTextArea(15,40);
 JButton sendMsgButton=new JButton();
 
 JLabel msgLabel=new JLabel();
 java.awt.List peopleList=new java.awt.List(15);
 
 
 Socket socket=null;
 PrintStream ps=null;
 ClientListener listener=null;
 
 //构造函数
 public CClient(){
  init();
 }
 
 //图形界面
 public void init(){
  this.setTitle("聊天室客户端");
  
  
  
  //初始化按钮和标签
  nameLabel.setText("姓名");
  ipLabel.setText("IP");
  connectButton.setText("连接");
  connectButton.addActionListener(this);
  disconnectButton.setText("断开");
  disconnectButton.addActionListener(this);
  
  //设置聊天内容不可编辑
  chatContentTextArea.setEditable(false);
  sendMsgButton.setText("发送");
  sendMsgButton.addActionListener(this);
  msgTextField.setText("请输入聊天信息");
  
  
  //pane1放置输入姓名和连接两个按钮
  JPanel panel1=new JPanel();
  panel1.setLayout(new FlowLayout());
  panel1.add(nameLabel);
  panel1.add(nameTextField);
  panel1.add(ipLabel);
  panel1.add(ipTextField);
  panel1.add(connectButton);
  panel1.add(disconnectButton);
  
  //用于放置聊天信息显示和聊天人员列表
  JPanel panel2=new JPanel();
  panel2.setLayout(new FlowLayout());
     JScrollPane pane1=new JScrollPane(chatContentTextArea);
    
 
  pane1.setBorder(new TitledBorder(BorderFactory.createEtchedBorder
  (Color.white,new Color(134,134,134)),"聊天内容"));
  
  
  panel2.add(pane1);
  
  JScrollPane pane2=new JScrollPane(peopleList);
     pane2.setBorder(new TitledBorder(BorderFactory.createEtchedBorder
  (Color.white,new Color(134,134,134)),"用户列表"));
  panel2.add(pane2);
  
  //用于放置发送信息区域
  
  JPanel panel3=new JPanel();
  panel3.setLayout(new FlowLayout());
  panel3.add(msgLabel);
  panel3.add(msgTextField);
  panel3.add(sendMsgButton);
  
  //将组件添加到界面
  this.getContentPane().setLayout(new BorderLayout());
  this.getContentPane().add(panel1,BorderLayout.NORTH);
  this.getContentPane().add(panel2, BorderLayout.CENTER);
  this.getContentPane().add(panel3, BorderLayout.SOUTH);
  
  this.pack();
  
  try{
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
   SwingUtilities.updateComponentTreeUI(this);
  }catch(Exception e){
   e.printStackTrace();
  }
  
  
 }
 
 
 //处理按钮事件
 public void actionPerformed(ActionEvent event){
  
   Object source=event.getSource();
   
   if(source==connectButton){
    //如果点击连接按钮
    if(socket==null){
     try{
      socket=new Socket(ipTextField.getText(),5000);
      System.out.println(socket);
      ps=new PrintStream(socket.getOutputStream());
      //pw=new PrintWriter(socket.getOutputStream());
      
     
      listener=new ClientListener(this,nameTextField.getText(),socket);
      listener.start();
     }catch(IOException e){
      System.out.println("Error:"+e);
      disconnect();
     }
    }
   }else if(source==disconnectButton){
    disconnect();
       
  }else if(source==sendMsgButton){
   
   String msg=msgTextField.getText();//nameTextField.getText()+InetAddress.getLocalHost().getHostAddress();+msgTextField.getText();
   ps.println(msg);
   ps.flush();
   //StringBuffer msg=new StringBuffer(nameTextField.getText()
   //).append("InetAddress.getLocalHost().getHostAddress()");
   
  
   //pw.println(msg);//.append(msgTextField.getText()));
     }
  }
  
 public void disconnect(){
   if(socket!=null){
    try{
     
     socket.close();
     listener.toStop();
     socket=null;
    }catch(IOException e){
     System.out.println("Error:"+e);
    }
   }
  }
 public static void main(String args[]){
   CClient client=new CClient();
   client.setVisible(true);
  }
  
  
  //客户端线程用来监听服务器传来的 信息
 class ClientListener extends Thread{
    String name=null;
    BufferedReader br=null;
    PrintStream ps=null;
    Socket socket=null;
    CClient parent=null;
    boolean running=true;
   
    //构造方法
    public ClientListener(CClient p,String n,Socket s){
     //接受参数
     parent=p;
     name=n;
     socket=s;
     
     try{
      br=new BufferedReader(new InputStreamReader(s.getInputStream()));
      ps=new PrintStream(s.getOutputStream());
     }catch(IOException e){
      System.out.println("ERROR:"+e);
      parent.disconnect();
     }
    }
   
   
    //停止监听
    public void toStop(){
     this.running=false;
    }
   
   
    //线程运行方法
    public void run(){
     String msg=null;
     while(running){
      msg=null;
      
      try{
       //读取从服务器传来的信息
       msg=br.readLine();
       System.out.println("receive msg:"+msg);
       parent.chatContentTextArea.append(msg);
       
     }catch(IOException e){
      System.out.println("ERROR:"+e);
      parent.disconnect();
     }
     
 
    }
     
      }

}
}