-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathAIO.java
More file actions
230 lines (200 loc) · 6.2 KB
/
AIO.java
File metadata and controls
230 lines (200 loc) · 6.2 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
-------------------
AIO |
-------------------
# jdk7的产物
# 才是真正的异步非阻塞IO,学习的是 Linux epoll 模式
# API
AsynchronousServerSocketChannel
# 服务端 Channel
public abstract <A> void accept(A attachment,CompletionHandler<AsynchronousSocketChannel,? super A> handler);
AsynchronousSocketChannel
# 客户端 Channel
public final <A> void read(ByteBuffer dst,A attachment,CompletionHandler<Integer,? super A> handler)
*
AsynchronousChannelGroup
# 线程组
ExecutorService
# 线程池,简单
CompletionHandler<V,A>
# 接口,俩抽象方法
void completed(V result, A attachment);
void failed(Throwable exc, A attachment);
-------------------
AIO-Server |
-------------------
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by Kevin on 2017/2/16 20:22.
*/
public class AioServer {
//线程池
private ExecutorService executorService;
//线程组
private AsynchronousChannelGroup asynchronousChannelGroup;
//服务器通道
private AsynchronousServerSocketChannel asynchronousServerSocketChannel;
public AioServer(int port){
try {
//实例化线程池
executorService = Executors.newCachedThreadPool();
//实例化线程组
asynchronousChannelGroup = AsynchronousChannelGroup.withCachedThreadPool(executorService,1);
//创建服务通道
asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open(asynchronousChannelGroup);
//绑定本地端口
asynchronousServerSocketChannel.bind(new InetSocketAddress(port));
System.out.println("服务端启动,port=" + port);
//进行阻塞
asynchronousServerSocketChannel.accept(this,new AioServerCompletionHandler());
//一直阻塞 不让服务器停止
Thread.sleep(Integer.MAX_VALUE);
}catch (Exception e){
e.printStackTrace();
}
}
public AsynchronousServerSocketChannel getAsynchronousServerSocketChannel() {
return asynchronousServerSocketChannel;
}
public void setAsynchronousServerSocketChannel(AsynchronousServerSocketChannel asynchronousServerSocketChannel) {
this.asynchronousServerSocketChannel = asynchronousServerSocketChannel;
}
}
-------------------
AIO-AioServerCompletionHandler|
-------------------
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;
/**
* Created by Kevin on 2017/2/16 20:27.
*/
public class AioServerCompletionHandler implements CompletionHandler<AsynchronousSocketChannel, AioServer> {
/**
* 连接成功的时候执行的操作
* @param result
* @param attachment
*/
@Override
public void completed(AsynchronousSocketChannel result, AioServer attachment) {
//当有下一个客户端接入的时候 直接调用Server的accept方法,这样反复执行下去,保证多个客户端都可以阻塞
attachment.getAsynchronousServerSocketChannel().accept(attachment,this);
read(result);
}
private void read(final AsynchronousSocketChannel asynchronousSocketChannel) {
//构建缓冲区
ByteBuffer buf = ByteBuffer.allocate(1024);
asynchronousSocketChannel.read(buf, buf, new CompletionHandler<Integer, ByteBuffer>() {
/**
* resultSize就是读取到的数据长度(字节数)
* @param resultSize
* @param attachment
*/
@Override
public void completed(Integer resultSize, ByteBuffer attachment) {
//进行读取之后,重置标识位
attachment.flip();
//获得读取的字节数
System.out.println("Server -> " + "收到客户端的数据长度为:" + resultSize);
//获取读取的数据
String resultData = new String(attachment.array()).trim();
System.out.println("Server -> " + "收到客户端的数据信息为:" + resultData);
String response = "服务器响应, 收到了客户端发来的数据: " + resultData;
write(asynchronousSocketChannel, response);
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
exc.printStackTrace();
}
});
}
private void write(AsynchronousSocketChannel asynchronousSocketChannel, String response) {
try {
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.put(response.getBytes());
buf.flip();
asynchronousSocketChannel.write(buf).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
/**
* 异常时执行
* @param exc
* @param attachment
*/
@Override
public void failed(Throwable exc, AioServer attachment) {
exc.printStackTrace();
}
}
-------------------
AIO-AioClient |
-------------------
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
/**
* Created by Kevin on 2017/2/16 20:38.
*/
public class AioClient implements Runnable{
private AsynchronousSocketChannel asc ;
public AioClient() throws Exception {
asc = AsynchronousSocketChannel.open();
}
public void connect(){
asc.connect(new InetSocketAddress("127.0.0.1", 1024));
}
public void write(String request){
try {
asc.write(ByteBuffer.wrap(request.getBytes())).get();
read();
} catch (Exception e) {
e.printStackTrace();
}
}
private void read() {
ByteBuffer buf = ByteBuffer.allocate(1024);
try {
asc.read(buf).get();
buf.flip();
byte[] respByte = new byte[buf.remaining()];
buf.get(respByte);
System.out.println(new String(respByte,"utf-8").trim());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true){
}
}
public static void main(String[] args) throws Exception {
AioClient c1 = new AioClient();
c1.connect();
AioClient c2 = new AioClient();
c2.connect();
AioClient c3 = new AioClient();
c3.connect();
new Thread(c1, "c1").start();
new Thread(c2, "c2").start();
new Thread(c3, "c3").start();
Thread.sleep(1000);
c1.write("c1 aaa");
c2.write("c2 bbbb");
c3.write("c3 ccccc");
}
}