導語:如何運用Java socket實現多人聊天室功能呢?下面是小編給大家提供的程式碼實現,大家可以參考閱讀,更多詳情請關注應屆畢業生考試網。
目錄結構:
ChatClient:
package ;import .*;
import t.*;
import .*;
import .*;
@SuppressWarnings("serial")
public class ChatClient extends Frame {
private TextArea ta = new TextArea();
private TextField tf = new TextField();
private DataOutputStream dos = null;
private DataInputStream dis = null;
private Socket socket = null;
private boolean bConnected = false;
private Thread thread=null;
public static void main(String[] args) {
new ChatClient()eClient();
}
public void frameClient(){
setSize(400, 400);
setLocation(400,300);
add(ta,H);
add(tf,H);
pack();
ctionListener(new TfListener());
//關閉視窗事件監聽
indowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
disconnected();
(0);
}
});
ect();
setVisible(true);
}
//連結服務器地址
private void connect(){
try {
socket = new Socket("", 8888);
thread=new Thread(new ChatThread());
t();
dos = new DataOutputStream(utputStream());
dis = new DataInputStream(nputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
tStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
tStackTrace();
}
}
//斷開連線
private void disconnected(){
bConnected = false;
try {
e();
e();
e();
} catch (IOException e1) {
// TODO Auto-generated catch block
tStackTrace();
}
}
//鍵盤迴車事件
private class TfListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String strMsg = ext();
ext("");
try {
eUTF(strMsg);
h();
} catch (IOException e1) {
// TODO Auto-generated catch block
tStackTrace();
}
}
}
//開啟執行緒接受伺服器資訊
private class ChatThread implements Runnable{
@Override
public void run() {
try {
bConnected = true;
while(bConnected){
String msg = UTF();
String taText = ext();
ext(taText+msg+"n");
}
} catch (SocketException e) {
tln("退出");;
} catch (IOException e) {
tStackTrace();
}
}
}
}
ChatServer:
package ;
import .*;
import .*;
import .*;
public class ChatServer {
private boolean started = false;
private List<ChatThread> chatThreads = new ArrayList<ChatThread>();
public static void main(String[] args) {
new ChatServer()tServer();
}
private void startServer(){
try {
//開啟服務端Socket
ServerSocket seso = new ServerSocket(8888);
started = true;
while(started){
//接受客戶端連線請求
Socket sos = pt();
tln("一個客戶端已連線");
//開啟執行緒處理客戶端通訊
ChatThread ct = new ChatThread(sos);
(ct);
new Thread(ct)t();
}
} catch (IOException e) {
tStackTrace();
}
}
private class ChatThread implements Runnable{
private Socket socket;
private DataInputStream din=null;
private DataOutputStream don=null;
private boolean bConnected = false;
public ChatThread(Socket socket) {
super();
et = socket;
}
//傳送資訊的函式
private void send(String strMsgIn){
try{
eUTF(strMsgIn);
h();
}catch(IOException e){
tStackTrace();
}
}
@Override
public void run() {
try{
din = new DataInputStream(nputStream());
don = new DataOutputStream(utputStream());
//讀取資料
bConnected = true;
while(bConnected){
String strMsgIn = UTF();
tln(strMsgIn);
//接收到資料後傳送給每個客戶端
for(int i =0;i<();i++){
(i)(strMsgIn);
}
}
}catch (IOException e) {
try {
//如果客戶端出錯或關閉,直接關閉連線,並移除List中的當前執行緒
e();
ve(this);
} catch (IOException e1) {
// TODO Auto-generated catch block
tStackTrace();
}
} finally{
try {
e();
e();
e();
} catch (IOException e) {
// TODO Auto-generated catch block
tStackTrace();
}
}
}
}
}
執行ChatSever後,再同時開啟多次ChatClient,就可以實現多人聊天了,你也試試。