練手小專案一:JAVA聊天室原始碼
阿新 • • 發佈:2019-01-30
客戶端:
- 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;
- publicclass Client 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 =
- publicstaticvoid main(String[] args) {
- new Client().creatFrame();
- }
- publicvoid creatFrame() { //產生圖形介面
- this.setBounds(300, 300, 300, 300);
- ta.setEditable(false);
- this.add(tf, BorderLayout.SOUTH);
- this.add(ta, BorderLayout.NORTH);
- this.addWindowListener(new WindowAdapter() {
- publicvoid windowClosing(WindowEvent e) {
- disconnect();
- System.exit(0);
- }
- });
- tf.addActionListener(new tfListener()); //響應輸入事件
- this.pack();
- this.setVisible(true);
- connect();
- new Thread(r).start();
- }
- publicvoid 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();
- }
- }
- publicvoid disconnect() { //視窗關閉時關閉客戶端,輸入,輸出
- try {
- dos.close();
- dis.close();
- s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- class tfListener implements ActionListener { //輸入框實現的介面,響應輸入事件
- publicvoid 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 { //客戶端執行緒接收資料
- publicvoid 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();
- }
- }
- }
- }
---------------------------------------------------------------------------------------------------------------------------------------
服務端:
- 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;
- publicclass Server {
- boolean started = false;
- ServerSocket ss = null;
- List<ChatClient> clients = new ArrayList<ChatClient>(); //儲存客戶端執行緒類
- publicstaticvoid 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();
- }
- }
- publicvoid 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();
- }
- }
- }
- }
- }