-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
109 lines (94 loc) · 3.75 KB
/
Server.java
File metadata and controls
109 lines (94 loc) · 3.75 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
package com.cat.multi.tcp;
import javax.crypto.Mac;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutionException;
/**
* Created by cat on 2018/1/21.
* Server
*/
public class Server {
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
System.out.println(
System.getProperty("os.name") + " , " +
System.getProperty("os.version") + " , " +
System.getProperty("os.arch")
);
// Mac OS X , 10.13.2 , x86_64
String addr = InetAddress.getLocalHost().getHostAddress();// 获得本机IP
System.out.println("self address==" + addr);
int port = 8888;
String host = "127.0.0.1";
// server
Server server = new Server(port);
server.service();
}
private final int port;
public Server(int port) {
this.port = port;
}
private boolean stop = false;
public void service() throws IOException, ExecutionException, InterruptedException {
ServerSocket ss = new ServerSocket(port);
System.out.println("开始监听连接过来的客户端:....");
while (true) {
if (stop) {
System.err.println("服务器终于要关闭了....");
break;
}
Socket accept = ss.accept();
new Thread(new HandleSocket(ss, accept)).start();
}
ss.close();
}
private class HandleSocket implements Runnable {
private final Socket accept;
private final ServerSocket ss;
public HandleSocket(ServerSocket ss, Socket socket) {
this.ss = ss;
this.accept = socket;
}
@Override
public void run() {
try {
InputStream in = accept.getInputStream(); // 这是阻塞方法? 不是!
// 读取客户端发送的数据
byte[] bytes = new byte[1024];
int read = 0;
String receiveFromClient = null;
if ((read = in.read(bytes)) != -1) {
String clientHost = accept.getInetAddress().getHostName();
// String clientAdd = accept.getInetAddress().getHostAddress();
receiveFromClient = new String(bytes, 0, read);
if (receiveFromClient.startsWith(":!q")) {
System.err.println("收到客户端要关闭服务器的请求了...");
// break; // 收到关闭服务器的消息,就不发消息给客户端了,直接退出服务器
stop = true;
accept.close();
Thread.currentThread().interrupt(); // 既然要关服务器了,把线程也停掉.
ss.close();
System.err.println("服务器关闭了...");
return;
}
System.out.println("from:" + clientHost + " , " + receiveFromClient);
} else {
System.err.println("TCP-Server read error:" + read);
}
// 每次收到数据,都给客户端一个响应:
OutputStream os = accept.getOutputStream();
String toClient = "toClient:" + receiveFromClient;
os.write(toClient.getBytes());
os.flush();
accept.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("---finally----");
}
}
}
}