基於GUI的簡單聊天室01

運用了Socket編程,gui,流的讀入和寫出,線程控制等java

思路:編程

一、首先是在客戶端中先創建好聊天的GUI服務器

二、創建服務器端,設置好端口號(用SocketServer),其中須要兩個boolean變量來分別表示服務器是否已經開啓和是否有客戶端鏈接進來,ide

  利用while循環來讓服務器在開啓的狀況下不斷接收客戶端的信息。利用DataOutputStream來進行通信ui

三、客戶端鏈接服務器(Socket)this

首先是客戶端的類spa

 1 import java.awt.BorderLayout;  2 import java.awt.event.ActionEvent;  3 import java.awt.event.ActionListener;  4 import java.awt.event.WindowAdapter;  5 import java.awt.event.WindowEvent;  6 import java.io.DataOutputStream;  7 import java.io.IOException;  8 import java.net.Socket;  9 import java.net.UnknownHostException;  10 
 11 import javax.swing.JFrame;  12 import javax.swing.JTextArea;  13 import javax.swing.JTextField;  14 
 15 /**
 16  * 完成圖形界面  17  * @author Administrator  18  *  19  */
 20 public class ChatClient extends JFrame{  21     JTextField jTextField = new JTextField();  22     JTextArea jTextArea = new JTextArea();  23  Socket s;  24  DataOutputStream bo;  25     public static void main(String[] args) {  26         new ChatClient().launchFrame();  27  }  28     
 29     public void launchFrame() {  30         setLocation(200, 150);  31         this.setSize(450, 450);  32         this.add(jTextArea,BorderLayout.NORTH);  33         this.add(jTextField,BorderLayout.SOUTH);  34         jTextField.addActionListener(new TFListener());  35         //pack();
 36         this.addWindowListener(new WindowAdapter() {  37  @Override  38             public void windowClosing(WindowEvent arg0) {  39  disConnect();  40                 System.exit(0);  41  }  42  });;  43         
 44         setVisible(true);  45  connect();  46  }  47     
 48     /**
 49  * 創建鏈接的方法  50  * @throws IOException  51  * @throws UnknownHostException  52      */
 53     public void connect() {  54          try {  55             s = new Socket("127.0.0.1",8888);  56             bo = new DataOutputStream(s.getOutputStream());  57         } catch (UnknownHostException e) {  58  e.printStackTrace();  59         } catch (IOException e) {  60  e.printStackTrace();  61  }  62          System.out.println("鏈接成功");  63  }  64     /**
 65  * 斷開鏈接,關閉資源的方法  66      */
 67     public void disConnect() {  68         try {  69  s.close();  70  bo.close();  71         } catch (IOException e1) {  72  e1.printStackTrace();  73  }  74         
 75  }  76     
 77     /**
 78  * 內部類,實現監聽  79  * 將文本框中的輸入打印到文本域中  80  *  81      */
 82     private class TFListener implements ActionListener{  83 
 84  @Override  85         public void actionPerformed(ActionEvent e) {  86             String content = jTextField.getText().trim();  87  jTextArea.setText(content);  88             jTextField.setText("");  89             //將文本發送到服務器
 90             try {  91  System.out.println(s);  92  bo.writeUTF(content);  93  bo.flush();  94             } catch (IOException e1) {  95  e1.printStackTrace();  96  }  97  }  98         
 99  } 100 }

而後是服務器的類.net

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatSever {
	public static void main(String[] args) {
		// 布爾類型變量表示服務器是否開着
		boolean started = false;
		ServerSocket ss = null;
		Socket s = null;
		DataInputStream bi = null;
		// 創建服務端,8888爲端口號
		try {
			ss = new ServerSocket(8888);
		} 
		catch (BindException e) {
			System.out.println("Socket has been used !");
			System.out.println("請重啓服務器 !");
			System.exit(0);
		}catch (IOException e) {
			e.printStackTrace();
		}
		// 服務器開啓後,started變爲true
		try {
			started = true;
			// 接受客戶端的鏈接
			while (started) {
				// 布爾類型變量bConnected表示有沒有用戶鏈接
				boolean bConnected = false;
				s = ss.accept();
				// 服務器鏈接後bConnected爲true
				bConnected = true;
				System.out.println("一個客戶鏈接");
				bi = new DataInputStream(s.getInputStream());
				while (bConnected) {
					String str = bi.readUTF();
					System.out.println(str);
					//bi.close();
				}
			}
		} catch (EOFException e) {
			System.out.println("Client close!");
		} catch (Exception e) {
			 e.printStackTrace();
		} finally {
			try {
				if (bi != null)
					bi.close();
				if (s != null)
					s.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}

		}
	}
	
}
相關文章
相關標籤/搜索