forked from jzj1993/JavaTcpSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpServer.java
More file actions
154 lines (139 loc) · 3.06 KB
/
TcpServer.java
File metadata and controls
154 lines (139 loc) · 3.06 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
147
148
149
150
151
152
153
154
package com.jzj.socket;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
* TCP Socket服务器端
*
* @author jzj1993
* @since 2015-2-22
*/
public abstract class TcpServer implements Runnable {
private int port;
private boolean runFlag;
private List<SocketTransceiver> clients = new ArrayList<SocketTransceiver>();
/**
* 实例化
*
* @param port
* 监听的端口
*/
public TcpServer(int port) {
this.port = port;
}
/**
* 启动服务器
* <p>
* 如果启动失败,会回调{@code onServerStop()}
*/
public void start() {
runFlag = true;
new Thread(this).start();
}
/**
* 停止服务器
* <p>
* 服务器停止后,会回调{@code onServerStop()}
*/
public void stop() {
runFlag = false;
}
/**
* 监听端口,接受客户端连接(新线程中运行)
*/
@Override
public void run() {
try {
final ServerSocket server = new ServerSocket(port);
while (runFlag) {
try {
final Socket socket = server.accept();
startClient(socket);
} catch (IOException e) {
// 接受客户端连接出错
e.printStackTrace();
this.onConnectFailed();
}
}
// 停止服务器,断开与每个客户端的连接
try {
for (SocketTransceiver client : clients) {
client.stop();
}
clients.clear();
server.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
// ServerSocket对象创建出错,服务器启动失败
e.printStackTrace();
}
this.onServerStop();
}
/**
* 启动客户端收发
*
* @param socket
*/
private void startClient(final Socket socket) {
SocketTransceiver client = new SocketTransceiver(socket) {
@Override
public void onReceive(InetAddress addr, String s) {
TcpServer.this.onReceive(this, s);
}
@Override
public void onDisconnect(InetAddress addr) {
clients.remove(this);
TcpServer.this.onDisconnect(this);
}
};
client.start();
clients.add(client);
this.onConnect(client);
}
/**
* 客户端:连接建立
* <p>
* 注意:此回调是在新线程中执行的
*
* @param client
* SocketTransceiver对象
*/
public abstract void onConnect(SocketTransceiver client);
/**
* 客户端:连接建立失败
* <p>
* 注意:此回调是在新线程中执行的
*/
public abstract void onConnectFailed();
/**
* 客户端:收到字符串
* <p>
* 注意:此回调是在新线程中执行的
*
* @param client
* SocketTransceiver对象
* @param s
* 字符串
*/
public abstract void onReceive(SocketTransceiver client, String s);
/**
* 客户端:连接断开
* <p>
* 注意:此回调是在新线程中执行的
*
* @param client
* SocketTransceiver对象
*/
public abstract void onDisconnect(SocketTransceiver client);
/**
* 服务器停止
* <p>
* 注意:此回调是在新线程中执行的
*/
public abstract void onServerStop();
}