Java实现简易聊天室

Server

package chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
public class Server {
    boolean started = false;
    ServerSocket ss = null;
    List<ChatClient> clients = new ArrayList<ChatClient>(); //保存客户端线程类 
   public static void main(String[] args) {
        new Server().start();
    }
    void start() {
        try {
            ss = new ServerSocket(8888); //建立服务端对象
            started = true;
        } catch (BindException e) {
            System.out.println("端口使用中");
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            while (started) {
                Socket s = ss.accept(); //接收客户端
               ChatClient c = new ChatClient(s);
                System.out.println("客戶端接收成功");
                new Thread(c).start(); //启动线程
                clients.add(c); //添加线程类
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ss.close();
            } catch (IOException e) {
                // TODO 自动生成 catch 块
               e.printStackTrace();
            }
        }
    }
    class ChatClient implements Runnable { //建立客户端线程接收,发送数据
        private Socket s;
        DataInputStream dis = null;
        DataOutputStream dos = null;
        boolean bConnected = false;
        public ChatClient(Socket s) {
           this.s = s;
            try {
                dis = new DataInputStream(s.getInputStream());
                dos = new DataOutputStream(s.getOutputStream());
                bConnected = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        void send(String str) {
            try {
                dos.writeUTF(str);
            } catch (SocketException e) {
                System.out.println("對方退出了");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public void run() {
            try {
                while (bConnected) {
                    String str = dis.readUTF();
                    // System.out.println(str); 
                    for (int i = 0; i < clients.size(); i++) {
                        ChatClient c = clients.get(i);
                        c.send(str);
                    }
                }
            } catch (EOFException e) {
               System.out.println("客戶端退出了");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (dis != null)
                    if (s != null)
                        try {
                            dis.close();
                            s.close();
                            dos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
            }
        }
    }
}

 client

package chat;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
public class ChatClient extends Frame { //繼承Frame类 
	TextField tf = new TextField(); //输入框对象 
    TextArea ta = new TextArea(); //显示框对象 
  Socket s = null;
    DataOutputStream dos = null;
    DataInputStream dis = null;
    boolean bConnected = false;
    recvThread r = new recvThread(); //线程类 
   public static void main(String[] args) {
        new ChatClient().creatFrame();
    }
    public void creatFrame() { //产生图形界面     
        this.setBounds(300, 300, 300, 300);
        ta.setEditable(false);
        this.add(tf, BorderLayout.SOUTH);
       this.add(ta, BorderLayout.NORTH);
        this.addWindowListener(new WindowAdapter() { //响应关闭窗口事件
                   public void windowClosing(WindowEvent e) {
                        disconnect();
                        System.exit(0);
                   }
               });
        tf.addActionListener(new tfListener()); //响应输入事件
      this.pack();
       this.setVisible(true);
        connect();
        new Thread(r).start();
    }
    public void connect() {
        try {
            s = new Socket("127.0.0.1", 8888); //建立客户端对象
       dos = new DataOutputStream(s.getOutputStream());
            dis = new DataInputStream(s.getInputStream());
            bConnected = true;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void disconnect() { //窗口关闭时关闭客户端,输入,输出 
       try {
            dos.close();
            dis.close();
           s.close();
       } catch (IOException e) {
           
            e.printStackTrace();
        }
    }
    class tfListener implements ActionListener { //输入框实现的接口,响应输入事件
        public void actionPerformed(ActionEvent e) {
            String str = tf.getText();
           //ta.setText(str); 
            tf.setText("");
            try {
                dos.writeUTF(str);
              dos.flush();
              //dos.close();
            } catch (IOException e1) {
               e1.printStackTrace();
         }
       }
    }
   class recvThread implements Runnable { //客户端线程接收数据
       public void run() {
           try {
                while (bConnected) {
                  String str;
                   str = dis.readUTF(); //拿到数据
                 ta.setText(ta.getText() + str + '\n');//显示到显示框中
               }
           } catch (SocketException e) {
            System.out.println("退出了");
          } catch (IOException e1) {
              e1.printStackTrace();
          }
        }
   }
}

猜你喜欢

转载自umgsai.iteye.com/blog/2353641
今日推荐