基于TCP的多人聊天室(二)

基于TCP的多人聊天室(二)

进行了封装,没有写单独的一个类,可自行实现,实现了多人聊天,没有实现私聊和一些细节问题,等有空再实现。

关闭流:

1
2
3
4
5
6
7
8
9
10
11
12
public class Closed {
public void close(Closeable... targets) {
for(Closeable target:targets) {
try {
if(null!=target)
target.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

聊天室服务器:

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
/**
* 聊天室服务器
*
*/
public class Chat_Server {
private static CopyOnWriteArrayList<Channel> list=new CopyOnWriteArrayList<>();
public static void main(String[] args) throws IOException {
System.out.println("-------Server-------");
// 1.指定端口,使用ServerSocket创建服务器
ServerSocket server = new ServerSocket(6666);
// 2.阻塞式等待链接 accept
while (true) {
Socket Server = server.accept();
System.out.println("客户端连接已建立");
Channel C=new Channel(Server);
list.add(C);
new Thread(C).start();
}
}
public CopyOnWriteArrayList<Channel> List(){
return list;

}
}
/**
* 一个客户端代表一个用户(Channel)
*
*/
class Channel implements Runnable{
private DataInputStream dis;
private DataOutputStream dos;
private Socket Server;
private CopyOnWriteArrayList<Channel> list=new Chat_Server().List();
private boolean flag = true;
public Channel(Socket server) {
Server = server;
// 3.操作:输入输出流
try {
dis = new DataInputStream(Server.getInputStream());
dos = new DataOutputStream(Server.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
// 4.释放资源
new Closed().close(dos,dis,Server);
this.flag=false;
}
}
//发送
private void send(String msg) {
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
// 4.释放资源
new Closed().close(dos,dis,Server);
this.flag=false;
}
}
//获取消息
private String getMsg() {
String msg = "";
try {
msg = dis.readUTF();
} catch (IOException e) {
new Closed().close(dos,dis,Server);
this.flag=false;
}
return msg;
}
//发给其他人
private void sendOther(String msg) {
for(Channel c:list) {
if(c==this) {
continue;
}
c.send(msg);
}
}
@Override
public void run() {
while(flag) {
String msg=getMsg();
if(!msg.equals("")) {
sendOther(msg);
}
}
}
}

聊天室客户端:

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
102
103
104
105
106
107
108
109
110
/**
* 聊天室客户端
*
*/
public class Chat_Client {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("-------Client-------");
// 1.建立连接:使用Socket创建客户端:服务器地址和端口
Socket Client = new Socket("localhost", 6666);
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入用户名:");
String name=bf.readLine();
//发送接收
new Thread(new Send(Client,name)).start();
new Thread(new Receive(Client)).start();
}
}
/**
* 发送端
*
*/
class Send implements Runnable{
private DataOutputStream dos;
private BufferedReader bf;
private Socket Client;
private boolean flag = true;
private String name;
public Send(Socket client,String name) {
Client = client;
this.name=name;
bf= new BufferedReader(new InputStreamReader(System.in));
try {
dos= new DataOutputStream(Client.getOutputStream());
} catch (IOException e) {
// 释放资源
new Closed().close(dos,bf,Client);
this.flag=false;
}
}
//发送
private void send(String msg) {
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
// 释放资源
new Closed().close(dos,bf,Client);
this.flag=false;
}
}
//控制台信息
private String getMsg() {
String msg = "";
try {
msg = bf.readLine();
} catch (IOException e) {
new Closed().close(dos,Client,bf);
}
return msg;
}
@Override
public void run() {
while (flag) {
String msg=getMsg();
if(!msg.equals("")) {
send(this.name+"说:"+msg);
}
}
}
}
/**
* 接收端
*
*/
class Receive implements Runnable{
private DataInputStream dis;
private Socket Client;
private boolean flag = true;
public Receive(Socket client) {
Client = client;
try {
dis= new DataInputStream(Client.getInputStream());
} catch (IOException e) {
// 释放资源
new Closed().close(dis,Client);
this.flag=false;
}
}
//接收
private String receive() {
String msg="";
try {
msg = dis.readUTF();
} catch (IOException e) {
// 释放资源
new Closed().close(dis,Client);
this.flag=false;
}
return msg;
}
@Override
public void run() {
while(flag) {
String msg=receive();
if(!msg.equals("")) {
System.out.println(msg);
}
}
}
}

简易实现基于TCP的网络聊天室。

原创技术分享,您的支持将鼓励我继续创作
0%