最近研究了下Java socket通信基础,利用代码实现了一个简单的多人聊天室功能,现把代码共享下,希望能帮到有兴趣了解的人。
目录结构:
ChatClient:
java" id="highlighter_716288">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
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 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());
//关闭窗口事件监听
this .addWindowListener( new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
disconnected();
System.exit( 0 );
}
});
this .connect();
setVisible( true );
}
//链接服务器地址
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//断开连接
private void disconnected(){
bConnected = false ;
try {
dos.close();
dis.close();
socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//键盘回车事件
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) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
//开启线程接受服务器信息
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
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 {
//开启服务端Socket
ServerSocket seso = new ServerSocket( 8888 );
started = true ;
while (started){
//接受客户端连接请求
Socket sos = seso.accept();
System.out.println( "一个客户端已连接" );
//开启线程处理客户端通信
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;
}
//发送信息的函数
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());
//读取数据
bConnected = true ;
while (bConnected){
String strMsgIn = din.readUTF();
System.out.println(strMsgIn);
//接收到数据后发送给每个客户端
for ( int i = 0 ;i<chatThreads.size();i++){
chatThreads.get(i).send(strMsgIn);
}
}
} catch (IOException e) {
try {
//如果客户端出错或关闭,直接关闭连接,并移除List中的当前线程
socket.close();
chatThreads.remove( this );
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} finally {
try {
din.close();
don.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
|
运行ChatSever后,再同时打开多次ChatClient,就可以实现多人聊天了,你也试试。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/eric_ley/article/details/54906931