-
Notifications
You must be signed in to change notification settings - Fork 2
/
NIOClient.java
86 lines (80 loc) · 2.42 KB
/
NIOClient.java
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
/**
*
*/
package zhangwenbo.net;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Scanner;
/**
* @author Iver3oN Zhang
* @date 2016年3月30日
* @email [email protected] NIOClient.java Impossible is nothing
*/
public class NIOClient {
// 定义检测SocketChannel的Selector对象
private Selector selector = null;
// 定义处理编码和解码的字符集
private Charset charset = Charset.forName("UTF-8");
// 客户端SocketChannel
private SocketChannel sc = null;
public void init() throws IOException {
selector = Selector.open();
InetSocketAddress isa = new InetSocketAddress("127.0.0.1", 30000);
// 调用open静态方法创建连接到指定主机的SocketChannel
sc = SocketChannel.open(isa);
// 设置该sc以非阻塞方式工作
sc.configureBlocking(false);
// 将SocketChannel对象注册到指定Selector
sc.register(selector, SelectionKey.OP_READ);
// 启动读取服务器端数据的线程
new ClientThread().start();
// 创建键盘输入流
Scanner scan = new Scanner(System.in);
while (scan.hasNextLine()) {
// 读取键盘输入
String line = scan.nextLine();
// 将键盘输入的内容输出到SocketChannel中
sc.write(charset.encode(line));
}
}
// 定义读取服务器数据的线程
private class ClientThread extends Thread {
public void run() {
try {
while (selector.select() > 0) {
// 遍历每个有可用IO操作Channel对应的SelectionKey
for (SelectionKey sk : selector.selectedKeys()) {
// 删除正在处理的SelectionKey
selector.selectedKeys().remove(sk);
// 如果该SelectionKey对应的Channel中有可读的数据
if (sk.isReadable()) {
// 使用NIO读取Channel中的数据
SocketChannel sc = (SocketChannel) sk.channel();
ByteBuffer buff = ByteBuffer.allocate(1024);
String content = "";
while (sc.read(buff) > 0) {
sc.read(buff);
buff.flip();
content += charset.decode(buff);
}
// 打印输出读取的内容
System.out.println(sk.toString()+":" + content);
// 为下一次读取作准备
sk.interestOps(SelectionKey.OP_READ);
}
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
new NIOClient().init();
}
}