SocketServer.java
3.93 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
package com.cnlive.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ConcurrentHashMap;
public class SocketServer {
/**
* 要处理客户端发来的对象,并返回一个对象,可实现该接口。
*/
public interface ObjectAction {
Object doAction(Object rev);
}
/*
* public static final class DefaultObjectAction implements ObjectAction{
* public Object doAction(Object rev) { System.out.println("处理并返回:"+rev);
* return rev; } }
*/
public static void main(String[] args) {
int port = 65432;
SocketServer server = new SocketServer(port);
server.start();
}
private int port;
private volatile boolean running = false;
private long receiveTimeDelay = 7000;
private ConcurrentHashMap<Class, ObjectAction> actionMapping = new ConcurrentHashMap<Class, ObjectAction>();
private Thread connWatchDog;
public SocketServer(int port) {
this.port = port;
}
public void start() {
if (running)
return;
running = true;
connWatchDog = new Thread(new ConnWatchDog());
connWatchDog.start();
}
@SuppressWarnings("deprecation")
public void stop() {
if (running)
running = false;
if (connWatchDog != null)
connWatchDog.stop();
}
public void addActionMap(Class<Object> cls, ObjectAction action) {
actionMapping.put(cls, action);
}
class ConnWatchDog implements Runnable {
public void run() {
try {
ServerSocket ss = new ServerSocket(port, 5);
while (running) {
System.out.println("等待客户端。。。");
Socket s = ss.accept();
new Thread(new SocketAction(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
SocketServer.this.stop();
}
}
}
class SocketAction implements Runnable {
Socket s;
boolean run = true;
long lastReceiveTime = System.currentTimeMillis();
public SocketAction(Socket s) {
this.s = s;
}
public void run() {
while (running && run) {
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
try {
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
byte[] recData = null;
while (true) {
recData = new byte[1024];
int r = in.read(recData);
if (r > -1) {
String data = new String(recData);
/*
* if(data.trim().equals("over")) { s.close();
* break; }
*/
System.out.println("读取到客户端发送的来数据:" + data);
}
}
/*
* if(in.available()>0){ BufferedReader br = new
* BufferedReader(new InputStreamReader(in)); //out = new
* PrintWriter(socket.getOutputStream(), true); String line
* = br.readLine(); System.out.println("you input is : " +
* line); //out.println("you input is :" + line);
* //out.close(); // in.close(); }
*/
/*
* ObjectInputStream ois = new ObjectInputStream(in); Object
* obj = ois.readObject(); lastReceiveTime =
* System.currentTimeMillis();
* System.out.println("接收:\t"+obj); ObjectAction oa =
* actionMapping.get(obj.getClass()); oa = oa==null?new
* DefaultObjectAction():oa; Object out = oa.doAction(obj);
* if(out!=null){ ObjectOutputStream oos = new
* ObjectOutputStream(s.getOutputStream());
* oos.writeObject(out); oos.flush(); }
*/
} catch (Exception e) {
System.out.println(System.currentTimeMillis() - lastReceiveTime);
e.printStackTrace();
overThis();
}
}
}
private void overThis() {
if (run)
run = false;
if (s != null) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("关闭:" + s.getRemoteSocketAddress());
}
}
}