-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
45 lines (40 loc) · 1.09 KB
/
Copy pathServer.java
File metadata and controls
45 lines (40 loc) · 1.09 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
package mysocket;
import java.io.IOException;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Author: wei1
* @Date: Create in 2018/11/18 15:54
* @Description:
*/
public class Server implements Serializable {
private static final long serialVersionUID = 1426844086965820852L;
private ServerSocket serverSocket;
public Server(int port) {
try {
serverSocket = new ServerSocket(port);
System.out.println("服务端启动成功");
} catch (IOException e) {
System.out.println("服务端启动失败");
}
}
public void start() {
new Thread(new Runnable() {
@Override
public void run() {
doStart();
}
}).start();
}
private void doStart() {
while (true) {
try {
Socket client = serverSocket.accept();
new ClientHandler(client).start();
} catch (IOException e) {
System.out.println("服务端异常");
}
}
}
}