简单的java聊天室

服务器
import java.net.*;
import java.io.*;
import java.util.*;
 
 
public class ChatServer {
 
static int port=5656;  //端口号
  static Vector clients=new Vector(10);   //存储连接客户信息
  static ServerSocket server=null;    //建立服务器socket
  static Socket socket=null;   //套接字连接
 
  public ChatServer() {                      
try {
  System.err.println("Server start...");
    server=new ServerSocket(port);    //初始化服务器套接字
while(true){   
    socket=server.accept();   //等待连接
    System.err.println(socket.getInetAddress()+"连接\n"); //得到客户机地址
    Client client=new Client(socket);  //实例化一个客户线程
   clients.addElement(client);  //增加客户线程到向量中          
  client.start();   //启动线程
notifyChatRoom();   //监视聊天室连接变化                                               
}
}
catch(Exception ex) {
    ex.printStackTrace(); //输出出错信息
}
  }
 
  public static void notifyChatRoom() {   
StringBuffer newUser=new StringBuffer("newUser");
for(int i=0;i
    Client c=(Client)clients.elementAt(i);
newUser.append(":"+c.name); //客户端姓名字符串  
}
sendClients(newUser);   //发送信息到客户端
  }
 
  public static void sendClients(StringBuffer message){ 
      for(int i=0;i
    Client client=(Client)clients.elementAt(i); //分别得到每个客户端的连接
client.send(message);  //发送信息
}
  }
 
  public void closeAll() {   //关闭所有连接
      while(clients.size()>0) {   //遍历整个Vector
Client client=(Client)clients.firstElement();  //得到一个客户端
try{
    client.socket.close(); //关闭端口
}
catch(IOException ex){
    ex.printStackTrace(); //输出错误信息
}
   clients.removeElement(client); //移出客户端信息
}
}
 
  public static void disconnect(Client c){     //断开客户端
try{
System.err.println(c.ip+"断开连接\n");
    c.send(new StringBuffer("QUIT"));   //向客户发送断开连接信息
c.socket=null; //关闭端口  
}
catch(Exception ex){
    ex.printStackTrace();
}
   clients.removeElement(c);   //移出客户端信息
  }
  
    public static void main(String[] args) {
    new ChatServer();      //实例化一个ChatServer类
  }
 
  class Client extends Thread  {  
   Socket socket;    //连接端口
String name;   //用户姓名
String ip;    //客户端IP地址
BufferedReader reader ;  //输入流
PrintStream ps;   //输出流   
public  Client(Socket s){  
  socket=s;                                           
  try{
      reader = new BufferedReader(new InputStreamReader(s.getInputStream())); //得到输入流
  ps=new PrintStream(s.getOutputStream());     //得到输出流
  String info=reader.readLine();   //读取接受到的信息
  StringTokenizer stinfo=new StringTokenizer(info,":");  //分解字符串
  String head=stinfo.nextToken();    //获取关键字
          if(stinfo.hasMoreTokens())   
  name=stinfo.nextToken();     //获取用户名
  if(stinfo.hasMoreTokens())
  ip=stinfo.nextToken();    //获取IP地址
  }
  catch(IOException ex){
      ex.printStackTrace();
  }
}
public void send(StringBuffer msg) {  
  ps.println(msg);   //输出信息
  ps.flush();
}
public void run(){    
  while(true){
      String line=null;
  try{
     line=reader.readLine();  //读取数据流       
  }
  catch(IOException ex){
      ex.printStackTrace(); //输出错误信息
  ChatServer.disconnect(this); //断开连接
  ChatServer.notifyChatRoom(); //更新信息
  return;
  }
  if(line==null){    //客户离开  
  ChatServer.disconnect(this);
  ChatServer.notifyChatRoom();
  return;
}
  
  StringTokenizer st=new StringTokenizer(line,":"); //分解字符串
  String keyword=st.nextToken();
  if(keyword.equals("MSG")){  //发送来的是聊天信息                  
      StringBuffer msg=new StringBuffer("MSG:");    
  msg.append(name);  //在信息上增加用户名
  msg.append(st.nextToken("\0"));
  ChatServer.sendClients(msg);      //发送聊天语句到各个客户端
  }
  else if(keyword.equals("QUIT")){ //退出命令  
      ChatServer.disconnect(this);   //断开连接 
  ChatServer.notifyChatRoom();   //刷新信息 
}
}
}     
}
}
 
 
 
 
客户端
 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.util.*;
 
public class ChatClient extends Applet {
    
  TextField tfName = new TextField(15); //姓名输入文本域
  Button btConnect = new Button("连接"); //连接按钮
  Button btDisconnect = new Button("断开连接");
  TextArea tfChat = new TextArea(8,27); //显示聊天信息文本框
  Button btSend = new Button("发送");
  TextField tfMessage = new TextField(30);  //聊天输入
  java.awt.List list1 = new java.awt.List(9); //显示在线用户信息
  
    Socket socket=null;  //连接端口
  PrintStream ps=null; //输出流
  Listen listen=null;  //监听线程
  
  public void init() {  //Applet初始化
   tfChat.setEditable(false); //设置信息显示框为不可编辑   
   Panel panel1 = new Panel();  //实例化面板     
  Panel panel2 = new Panel();        
  Panel panel3 = new Panel();
  tfChat.setBackground(Color.white);  //设置组件背景颜色
  panel1.setBackground(Color.orange);
  panel2.setBackground(Color.pink);    
   panel3.setBackground(Color.orange);    
   panel1.add(new Label("姓名:"));  //增加组件到面板上
   panel1.add(tfName);
   panel1.add(btConnect);
   panel1.add(btDisconnect);    
   panel2.add(tfChat);
   panel2.add(list1);    
   panel3.add(new Label("聊天信息"));
   panel3.add(tfMessage);
   panel3.add(btSend);    
   setLayout(new BorderLayout()); //设置Applet布局管理器
   add(panel1, BorderLayout.NORTH);  //增加面板到Applet上
   add(panel2, BorderLayout.CENTER);
   add(panel3,  BorderLayout.SOUTH);
  }
 
  public boolean action(Event evt,Object obj){    //事件处理
  try{
if(evt.target==btConnect){   //点击连接按钮
   if (socket==null){
socket=new Socket(InetAddress.getLocalHost(),5656);     //实例化一个套接字                 
ps=new PrintStream(socket.getOutputStream());   //获取输出流
StringBuffer info=new StringBuffer("INFO: ");                                                            
String userinfo=tfName.getText()+":"+InetAddress.getLocalHost().toString();
ps.println(info.append(userinfo)); //输出信息
ps.flush();
listen=new Listen(this,tfName.getText(),socket);    //实例化监听线程
listen.start();     //启动线程
}   
}
  else if(evt.target==btDisconnect){    //点击断开连接按钮
        disconnect();  //调用断开连接方法
   }
  else if(evt.target==btSend){   //点击发送按钮
        if(socket!=null){
    StringBuffer msg=new StringBuffer("MSG: ");     
String msgtxt=new String(tfMessage.getText());
ps.println(msg.append(tfMessage.getText()));   //发送信息  
ps.flush();
}
  }
}
catch (Exception ex){
ex.printStackTrace();  //输出错误信息
}
    return true;
  }   
 
  public void disconnect() {   //断开连接方法
      if(socket!=null){
ps.println("QUIT");  //发送信息
ps.flush();
}
  }
 
  class Listen extends Thread{    //监听服务器传送的信息
  String name=null;          //用户名
BufferedReader reader ;    //输入流
  PrintStream ps=null;     //输出流
  Socket socket=null;      //本地套接字
  ChatClient client=null;   //客户端ChatClient实例
 
  public Listen(ChatClient p,String n,Socket s) {  
client=p;
name=n;
socket=s;
 
try{
   reader = new BufferedReader(new InputStreamReader(s.getInputStream())); //获取输入流
ps=new PrintStream(s.getOutputStream());  //获取输出流
 
}
catch(IOException ex){
    client.disconnect(); //出错则断开连接
    ex.printStackTrace(); //输出错误信息
  }
    }  
  
  public void run(){  
      String msg=null;
  while(socket!=null){
    try{
    msg=reader.readLine();  //读取服务器端传来信息
    }                 
catch(IOException ex){
    client.disconnect(); //出错则断开连接
    ex.printStackTrace(); //输出错误信息
}
if (msg==null) {    //从服务器传来的信息为空则断开此次连接
  client.listen=null;              
  client.socket=null;
  client.list1.removeAll();
  return;
}
StringTokenizer st=new StringTokenizer(msg,":");   //分解字符串
String keyword=st.nextToken();         
 
if(keyword.equals("newUser")) {    //新用户连接信息
    client.list1.removeAll();  //移除原有用户名
while(st.hasMoreTokens()) {    //取得目前所有聊天室用户名
    String str=st.nextToken();
client.list1.add(str);  //增加到用户列表中
}
}
else if(keyword.equals("MSG")) {    //聊天信息                                                 
    String usr=st.nextToken();
client.tfChat.append(usr);  //增加聊天信息到信息显示框
client.tfChat.append(st.nextToken("\0"));
client.tfChat.append("\n");  
}
else if(keyword.equals("QUIT")) {   //断天连接信息  
    System.out.println("Quit");
    try{
      client.listen=null;
      client.socket.close();  //关闭端口
  client.socket=null;
              }
              catch(IOException e){}
  client.list1.removeAll();  //移除用户列表  
return;
}
  }
  }     
}     

posted on 2016-05-16 23:54  Honey_Badger  阅读(283)  评论(0编辑  收藏  举报

导航

github