写在前面
对于Java NIO已经学习了一段时间了,周末实践了下,折腾了一天,总算对NIO的理论,有了一个感性的认识。下面的实践是:服务器与客户端都采用NIO的方式来实现文件下载。对于传统的SOCKET BIO方式,服务器端会为每个连接上的客户端分配一个Worker线程来进行doWork,而NIO SERVER却没有为每个Socket链接分配线程的必要了,避免了大量的线程所需的上下文切换,借助NIO提供的Selector机制,只需要一个或者几个线程来管理成百上千的SOCKET连接。那么下面我们就来看看吧!
文件下载辅助类
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
|
/**
* 这个类的基本思路是,读取本地文件到缓冲区
* 因为通道只能操作缓冲区
*/
class
DownloadFileProcesser
implements
Closeable{
private
ByteBuffer buffer = ByteBuffer.allocate(
8
*
1024
);
private
FileChannel fileChannel ;
public
DownloadFileProcesser() {
try
{
FileInputStream fis =
new
FileInputStream(
"e:/tmp/Shell学习笔记.pdf"
);
fileChannel = fis.getChannel();
}
catch
(Exception e){
e.printStackTrace();
}
}
public
int
readFile2Buffer()
throws
IOException{
int
count =
0
;
buffer.clear();
count = fileChannel.read(buffer);
buffer.flip();
return
count;
}
public
ByteBuffer getByteBuffer(){
return
buffer;
}
@Override
public
void
close()
throws
IOException {
fileChannel.close();
}
}
|
服务端代码:
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
|
public
class
ServerMain {
public
static
void
main(String[] args)
throws
IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(
false
);
serverSocketChannel.socket().bind(
new
InetSocketAddress(
8887
));
Selector selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while
(
true
) {
selector.select();
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while
(iterator.hasNext()) {
SelectionKey s = iterator.next();
// 如果客户端有连接请求
if
(s.isAcceptable()) {
System.out.println(
"客户端连接请求.."
);
ServerSocketChannel ssc = (ServerSocketChannel) s.channel();
SocketChannel sc = ssc.accept();
sc.configureBlocking(
false
);
sc.register(selector, SelectionKey.OP_READ);
}
// 如果客户端有发送数据请求
if
(s.isReadable()) {
System.out.println(
"接受客户端发送过来的文本消息..."
);
//这里拿出的通道就是ACCEPT上注册的SocketChannel通道
SocketChannel sc = (SocketChannel) s.channel();
//要读取数据先要准备好BUFFER缓冲区
ByteBuffer buffer = ByteBuffer.allocate(
8
*
1024
);
//准备BYTE数组,形成输出
sc.read(buffer);
byte
[] clientByteInfo =
new
byte
[buffer.position()];
buffer.flip();
buffer.get(clientByteInfo);
System.out.println(
"服务器端收到消息:"
+
new
String(clientByteInfo,
"utf-8"
));
//CLIENT下一步的动作就是读取服务器端的文件,因此需要注册写事件
SelectionKey selectionKey = sc.register(selector, SelectionKey.OP_WRITE);
//在这个selectionKey上绑定一个对象,以供写操作时取出进行处理
DownloadFileProcesser downloadFileProcesser =
new
DownloadFileProcesser();
selectionKey.attach(downloadFileProcesser);
}
// 如果客户端有下载文件数据请求
if
(s.isWritable()) {
//这里把attachment取出进行写入操作
DownloadFileProcesser downloadFileProcesser = (DownloadFileProcesser)s.attachment();
int
count = downloadFileProcesser.readFile2Buffer();
if
(count <=
0
){
System.out.println(
"客户端下载完毕..."
);
//关闭通道
s.channel().close();
downloadFileProcesser.close();
}
else
{
//需要注意的是我们这里并没有出现常见的while写的结构,这是为何?
//因为client其实不断的在read操作,从而触发了SELECTOR的不断写事件!
SocketChannel sc = (SocketChannel)s.channel();
sc.write(downloadFileProcesser.getByteBuffer());
}
}
iterator.remove();
}
}
}
}
|
客户端代码:
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
|
class
Client4DownloadFile
implements
Runnable{
//标示
private
String name;
private
FileChannel fileChannel;
public
Client4DownloadFile(String name , RandomAccessFile randomAccessFile){
this
.name = name;
this
.fileChannel = randomAccessFile.getChannel();
}
private
ByteBuffer buffer = ByteBuffer.allocate(
8
*
1024
);
@Override
public
void
run() {
try
{
SocketChannel sc = SocketChannel.open();
Selector selector = Selector.open();
sc.configureBlocking(
false
);
sc.register(selector, SelectionKey.OP_CONNECT);
sc.connect(
new
InetSocketAddress(
"127.0.0.1"
,
8887
));
while
(
true
){
selector.select();
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while
(iterator.hasNext()){
SelectionKey s = iterator.next();
if
(s.isConnectable()){
System.out.println(
"客户端["
+ name +
"]已经连接上了服务器..."
);
SocketChannel sc2 = (SocketChannel)s.channel();
if
(sc2.isConnectionPending() && sc2.finishConnect()){
sc2.configureBlocking(
false
);
String msg =
"Thread-"
+ name +
" send message!"
;
byte
[] b = msg.getBytes(
"utf-8"
);
sc2.write(ByteBuffer.wrap(b));
System.out.println(
"客户端["
+ name +
"]给服务器端发送文本消息完毕..."
);
sc2.register(selector, SelectionKey.OP_READ);
}
}
if
(s.isReadable()){
SocketChannel sc3 = (SocketChannel)s.channel();
buffer.clear();
int
count = sc3.read(buffer);
if
(count <=
0
){
s.cancel();
System.out.println(
"Thread "
+ name +
" 下载完毕..."
);
}
while
(count >
0
){
buffer.flip();
fileChannel.write(buffer);
count = sc3.read(buffer);
}
}
iterator.remove();
}
}
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|
1
2
3
4
5
6
7
8
9
10
|
public
class
ClientMain {
public
static
void
main(String[] args)
throws
FileNotFoundException{
for
(
int
i =
0
; i <
10
; i++){
File file =
new
File(
"e:/tmp/"
+ i +
".pdf"
);
RandomAccessFile raf =
new
RandomAccessFile(file,
"rw"
);
Client4DownloadFile client4DownloadFile =
new
Client4DownloadFile(
""
+ i, raf);
new
Thread(client4DownloadFile).start();
}
}
}
|
本文转自zfz_linux_boy 51CTO博客,原文链接:http://blog.51cto.com/zhangfengzhe/1726488,如需转载请自行联系原作者