一個Java實現的簡單的多個客戶端聊天程序
客戶端
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.net.*;
public class HeartClient extends Frame {
/*
* 成員方法出場...
*/
private TextField tfText;
private TextArea taContent;
private Socket s;
private DataOutputStream dos;
private DataInputStream dis;
/**
* 注意,入口... ^^
* @param args
*/
public static void main(String[] args) {
new HeartClient().launchFrame();
}
/**
* Loading GU
*/
public void launchFrame(){
tfText = new TextField();
taContent = new TextArea();
this.setSize(300,300);
this.setLocation(300,300);
this.tfText.addActionListener(new TFListener());
this.add(tfText,BorderLayout.SOUTH);
this.add(taContent,BorderLayout.NORTH);
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
this.pack();
this.connect();
this.setVisible(true);
}
/**
* 我在努力地連接服務器中...
*/
public void connect() {
try {
s = new Socket("127.0.0.1",1720);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
new Thread(new SendThread()).start();
// dos.writeUTF("Hello,i find u!");
} catch (UnknownHostException e) {
System.out.println("UnknownHostException");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
}finally{
//關閉啥尼...
}
}
/**
* 額不會傻等著tfText(TextField tfText的監聽器類)
*/
class TFListener implements ActionListener{
private String str;
@Override
public void actionPerformed(ActionEvent e) {
str = tfText.getText().trim();
tfText.setText("");
try {
dos.writeUTF(str);
} catch (IOException e1) {
System.out.println("IOException");
e1.printStackTrace();
}
}
}
/**
* 客戶端接收消息的線程呦...
*
*/
class SendThread implements Runnable{
private String str;
private boolean iConnect = false;
public void run(){
iConnect = true;
recMsg();
}
/**
* 消息,看招,哪里跑...(客戶端接收消息的實現)
* @throws IOException
*/
public void recMsg() {
try {
while(iConnect){
str = dis.readUTF();
taContent.setText(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服務器端
import java.io.*;
import java.net.*;
import java.util.*;
public class HeartServer {
/*
* 成員變量閃亮登場
*/
List<ClientThread> clients = new ArrayList<ClientThread>();
/**
* 這系入口啊,向這里看齊...
* @param args
*/
public static void main(String[] args) {
new HeartServer().start();
}
/**
* 啟動服務器中...
*
*/
public void start(){
try {
boolean iConnect = false;
ServerSocket ss = new ServerSocket(1720);
iConnect = true;
while(iConnect){
System.out.println("綁定服務器端口成功!");
Socket s = ss.accept();
ClientThread currentClient = new ClientThread(s);//創建線程引用
System.out.println("發現客戶端!");
clients.add(currentClient);//把當前客戶端加入集合
new Thread(currentClient).start();
System.out.println("客戶端進程已經啟動!");
}
} catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
}
}
/**
* 給每個客戶端留個家(客戶端的進程)
*
*/
class ClientThread implements Runnable {
/*
* 成員變量又來啦...
*/
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private String str;
private boolean iConnect = false;
/**
* 小構一下
*/
ClientThread(Socket s){
this.s = s;
iConnect = true;
}
public void run(){
System.out.println("run方法啟動了!");
try {
while(iConnect){
System.out.println("RUN方法中的while循環啟動,正在等待客戶端的發送消息...");
dis = new DataInputStream(s.getInputStream());
str = dis.readUTF();
System.out.println(str);
for(int i=0; i<clients.size(); i++){
System.out.println("轉發消息中..."+i);
ClientThread c = clients.get(i);
c.sendMsg(str);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 轉發消息,我做主...
* 將送至服務器的消息發送給每個連接到的客戶端
*/
public void sendMsg(String str){
try {
System.out.println("創建輸出管道!");
dos = new DataOutputStream(this.s.getOutputStream());
System.out.println("正在向客戶端寫消息!");
dos.writeUTF(str);
System.out.println("正在向客戶端寫消息成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
本文由用戶 SimoneNewco 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!