如何運用Java socket實現(xiàn)多人聊天室功能
導(dǎo)語:如何運用Java socket實現(xiàn)多人聊天室功能呢?下面是小編給大家提供的代碼實現(xiàn),大家可以參考閱讀,更多詳情請關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。
目錄結(jié)構(gòu):
ChatClient:
package com.panda.chat;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
@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().frameClient();
}
public void frameClient(){
setSize(400, 400);
setLocation(400,300);
add(ta,BorderLayout.NORTH);
add(tf,BorderLayout.SOUTH);
pack();
tf.addActionListener(new TfListener());
/pic/p>
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
disconnected();
System.exit(0);
}
});
this.connect();
setVisible(true);
}
/pic/p>
private void connect(){
try {
socket = new Socket("127.0.0.1", 8888);
thread=new Thread(new ChatThread());
thread.start();
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
} catch (UnknownHostException e) {
/pic/p>
e.printStackTrace();
} catch (IOException e) {
/pic/p>
e.printStackTrace();
}
}
/pic/p>
private void disconnected(){
bConnected = false;
try {
dos.close();
dis.close();
socket.close();
} catch (IOException e1) {
/pic/p>
e1.printStackTrace();
}
}
/pic/p>
private class TfListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String strMsg = tf.getText();
tf.setText("");
try {
dos.writeUTF(strMsg);
dos.flush();
} catch (IOException e1) {
/pic/p>
e1.printStackTrace();
}
}
}
/pic/p>
private class ChatThread implements Runnable{
@Override
public void run() {
try {
bConnected = true;
while(bConnected){
String msg = dis.readUTF();
String taText = ta.getText();
ta.setText(taText+msg+"\n");
}
} catch (SocketException e) {
System.out.println("退出");;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ChatServer:
package com.panda.chat;
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
private boolean started = false;
private List<ChatThread> chatThreads = new ArrayList<ChatThread>();
public static void main(String[] args) {
new ChatServer().startServer();
}
private void startServer(){
try {
/pic/p>
ServerSocket seso = new ServerSocket(8888);
started = true;
while(started){
/pic/p>
Socket sos = seso.accept();
System.out.println("一個客戶端已連接");
/pic/p>
ChatThread ct = new ChatThread(sos);
chatThreads.add(ct);
new Thread(ct).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
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();
this.socket = socket;
}
/pic/p>
private void send(String strMsgIn){
try{
don.writeUTF(strMsgIn);
don.flush();
}catch(IOException e){
e.printStackTrace();
}
}
@Override
public void run() {
try{
din = new DataInputStream(socket.getInputStream());
don = new DataOutputStream(socket.getOutputStream());
/pic/p>
bConnected = true;
while(bConnected){
String strMsgIn = din.readUTF();
System.out.println(strMsgIn);
/pic/p>
for(int i =0;i<chatThreads.size();i++){
chatThreads.get(i).send(strMsgIn);
}
}
}catch (IOException e) {
try {
/pic/p>
socket.close();
chatThreads.remove(this);
} catch (IOException e1) {
/pic/p>
e1.printStackTrace();
}
} finally{
try {
din.close();
don.close();
socket.close();
} catch (IOException e) {
/pic/p>
e.printStackTrace();
}
}
}
}
}
運行ChatSever后,再同時打開多次ChatClient,就可以實現(xiàn)多人聊天了,你也試試。
【如何運用Java socket實現(xiàn)多人聊天室功能】相關(guān)文章:
Java如何通過Socket實現(xiàn)TCP服務(wù)端10-01
java如何實現(xiàn)后臺自動發(fā)郵件功能03-09
講解Java的Socket網(wǎng)絡(luò)編程的多播與廣播實現(xiàn)02-11
java實現(xiàn)后臺自動發(fā)郵件功能11-01
java如何實現(xiàn)漢諾塔01-31
Java中如何實現(xiàn)顯示動態(tài)的時間03-14
java通用組合算法如何實現(xiàn)10-04