-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketServerDemo2.java
More file actions
146 lines (130 loc) · 3.55 KB
/
Copy pathSocketServerDemo2.java
File metadata and controls
146 lines (130 loc) · 3.55 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.java.study;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class SocketServerDemo2 extends ServerSocket {
private static final int SERVER_PORT = 2015;
private static boolean isPrint = false;// 是否输出消息标志
private static List user_list = new ArrayList();// 登录用户集合
private static List<ServerThread> thread_list = new ArrayList<ServerThread>();// 服务器已启用线程集合
private static LinkedList<String> message_list = new LinkedList<String>();// 存放消息队列
/**
* 创建服务端Socket,创建向客户端发送消息线程,监听客户端请求并处理
*/
public SocketServerDemo2() throws IOException {
super(SERVER_PORT);// 创建ServerSocket
new PrintOutThread();// 创建向客户端发送消息线程
try {
while (true) {// 监听客户端请求,启个线程处理
Socket socket = accept();
new ServerThread(socket);
}
} catch (Exception e) {
} finally {
close();
}
}
/**
* 监听是否有输出消息请求线程类,向客户端发送消息
*/
class PrintOutThread extends Thread {
public PrintOutThread() {
start();
}
@Override
public void run() {
while (true) {
if (isPrint) {// 将缓存在队列中的消息按顺序发送到各客户端,并从队列中清除。
String message = message_list.getFirst();
for (ServerThread thread : thread_list) {
thread.sendMessage(message);
}
message_list.removeFirst();
isPrint = message_list.size() > 0 ? true : false;
}
}
}
}
/**
* 服务器线程类
*/
class ServerThread extends Thread {
private Socket client;
private PrintWriter out;
private BufferedReader in;
private String name;
public ServerThread(Socket s) throws IOException {
client = s;
out = new PrintWriter(client.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
client.getInputStream()));
in.readLine();
out.println("成功连上聊天室,请输入你的名字:");
start();
}
@Override
public void run() {
try {
int flag = 0;
String line = in.readLine();
while (!"byeClient".equals(line)) {
// 查看在线用户列表
if ("showuser".equals(line)) {
out.println(this.listOnlineUsers());
line = in.readLine();
}
// 第一次进入,保存名字
if (flag++ == 0) {
name = line;
user_list.add(name);
thread_list.add(this);
out.println(name + "你好,可以开始聊天了...");
this.pushMessage("Client<" + name + ">进入聊天室...");
} else {
this.pushMessage("Client<" + name + "> say : " + line);
}
line = in.readLine();
}
out.println("byeClient");
} catch (Exception e) {
e.printStackTrace();
} finally {// 用户退出聊天室
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
thread_list.remove(this);
user_list.remove(name);
pushMessage("Client<" + name + ">退出了聊天室");
}
}
// 放入消息队列末尾,准备发送给客户端
private void pushMessage(String msg) {
message_list.addLast(msg);
isPrint = true;
}
// 向客户端发送一条消息
private void sendMessage(String msg) {
out.println(msg);
}
// 统计在线用户列表
private String listOnlineUsers() {
String s = "--- 在线用户列表 ---\015\012";
for (int i = 0; i < user_list.size(); i++) {
s += "[" + user_list.get(i) + "]\015\012";
}
s += "--------------------";
return s;
}
}
public static void main(String[] args) throws IOException {
new SocketServerDemo2();// 启动服务端
}
}