-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
127 lines (112 loc) · 4.04 KB
/
Client.java
File metadata and controls
127 lines (112 loc) · 4.04 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
package com.hlkj.baojin;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ConcurrentHashMap;
/**
* C/S架构的客户端对象,持有该对象,可以随时向服务端发送消息。
*/
public class Client {
/**
* 处理服务端发回的对象,可实现该接口。
*/
public interface ObjectAction {
void doAction(Object obj, Client client);
}
public static final class DefaultObjectAction implements ObjectAction {
public void doAction(Object obj, Client client) {
System.out.println("处理:\t" + obj.toString());
}
}
public static void main(String[] args) throws UnknownHostException, IOException {
String serverIp = "127.0.0.1";
int port = 65432;
Client client = new Client(serverIp, port);
client.start();
}
private String serverIp;
private int port;
private Socket socket;
private boolean running = false;
private long lastSendTime;
private ConcurrentHashMap<Class, ObjectAction> actionMapping = new ConcurrentHashMap<Class, ObjectAction>();
public Client(String serverIp, int port) {
this.serverIp = serverIp;
this.port = port;
}
public void start() throws UnknownHostException, IOException {
if (running) return;
socket = new Socket(serverIp, port);
// socket.set
System.out.println("本地端口:" + socket.getLocalPort());
lastSendTime = System.currentTimeMillis();
running = true;
new Thread(new KeepAliveWatchDog()).start();
new Thread(new ReceiveWatchDog()).start();
}
public void stop() {
if (running) running = false;
}
/**
* 添加接收对象的处理对象。
*
* @param cls 待处理的对象,其所属的类。
* @param action 处理过程对象。
*/
public void addActionMap(Class<Object> cls, ObjectAction action) {
actionMapping.put(cls, action);
}
public void sendObject(Object obj) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(obj);
System.out.println("发送:\t" + obj);
oos.flush();
}
class KeepAliveWatchDog implements Runnable {
long checkDelay = 10;
long keepAliveDelay = 2000;
public void run() {
while (running) {
if (System.currentTimeMillis() - lastSendTime > keepAliveDelay) {
try {
Client.this.sendObject(new KeepAlive());
Client.this.sendObject("hello");
} catch (IOException e) {
e.printStackTrace();
Client.this.stop();
}
lastSendTime = System.currentTimeMillis();
} else {
try {
Thread.sleep(checkDelay);
} catch (InterruptedException e) {
e.printStackTrace();
Client.this.stop();
}
}
}
}
}
class ReceiveWatchDog implements Runnable {
public void run() {
while (running) {
try {
InputStream in = socket.getInputStream();
if (in.available() > 0) {
ObjectInputStream ois = new ObjectInputStream(in);
Object obj = ois.readObject();
System.out.println("接收:\t" + obj);
ObjectAction oa = actionMapping.get(obj.getClass());
oa = oa == null ? new DefaultObjectAction() : oa;
oa.doAction(obj, Client.this);
} else {
Thread.sleep(10);
}
} catch (Exception e) {
e.printStackTrace();
Client.this.stop();
}
}
}
}
}