Skip to content

Commit b159f65

Browse files
committed
将文章添加到note中
1 parent 65ece2a commit b159f65

File tree

21 files changed

+7308
-0
lines changed

21 files changed

+7308
-0
lines changed

Spring-Netty/src/main/java/com/bruis/learnnetty/bio/Client.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
public class Client {
2121
private static final String HOST = "127.0.0.1";
2222
private static final int PORT = 8000;
23+
// 休眠时间
2324
private static final int SLEEP_TIME = 5000;
25+
// 最大数据大小
2426
public static final int MAX_DATA_LEN = 1024;
2527

2628
public static void main(String[] args) throws IOException {
@@ -31,10 +33,13 @@ public static void main(String[] args) throws IOException {
3133
while (true) {
3234
try {
3335
Scanner scan = new Scanner(System.in);
36+
// 读取控制台输入信息
3437
String clientMessage = scan.nextLine();
3538
System.out.println("客户端发送数据: " + clientMessage);
39+
// 将数据写入流中
3640
socket.getOutputStream().write(clientMessage.getBytes());
3741

42+
// 将数据从JVM中读取出来,存放在输入流中
3843
InputStream inputStream = socket.getInputStream();
3944

4045
byte[] data = new byte[MAX_DATA_LEN];
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.bruis.learnnetty.nio.demo01;
2+
3+
import java.net.InetSocketAddress;
4+
import java.nio.ByteBuffer;
5+
import java.nio.channels.SocketChannel;
6+
7+
/**
8+
* @author LuoHaiYang
9+
*/
10+
public class NIOClient {
11+
12+
public static void main(String[] args) throws Exception {
13+
14+
// 获取一个SocketChannel
15+
SocketChannel socketChannel = SocketChannel.open();
16+
17+
// 设置非阻塞
18+
socketChannel.configureBlocking(false);
19+
20+
// 提供服务端的ip和端口
21+
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6666);
22+
23+
// 连接服务器
24+
if (!socketChannel.connect(inetSocketAddress)) {
25+
while (!socketChannel.finishConnect()) {
26+
System.out.println("因为连接需要时间, 客户端不会阻塞,可以做其他工作了...");
27+
}
28+
}
29+
30+
// 如果连接成功,就发送数据
31+
String str = "hello, bruis~";
32+
33+
// 把字节数组包装成buffer
34+
ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
35+
36+
// 发送数据,将buffer数据写入channel
37+
socketChannel.write(buffer);
38+
39+
System.in.read();
40+
}
41+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.bruis.learnnetty.nio.demo01;
2+
3+
import java.net.InetSocketAddress;
4+
import java.nio.ByteBuffer;
5+
import java.nio.channels.SelectionKey;
6+
import java.nio.channels.Selector;
7+
import java.nio.channels.ServerSocketChannel;
8+
import java.nio.channels.SocketChannel;
9+
import java.util.Iterator;
10+
import java.util.Set;
11+
12+
/**
13+
* @author LuoHaiYang
14+
*/
15+
public class NIOServer {
16+
17+
public static void main(String[] args) throws Exception {
18+
19+
// 创建ServerSocketChannel -> ServerSocket
20+
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
21+
22+
// 得到一个Selector对象
23+
Selector selector = Selector.open();
24+
25+
// 绑定一个端口:6666,在服务端监听
26+
serverSocketChannel.socket().bind(new InetSocketAddress(6666));
27+
28+
// 设置为非阻塞
29+
serverSocketChannel.configureBlocking(false);
30+
31+
// 把ServerSocketChannel注册到selector, 事件为:OP_ACCEPT
32+
SelectionKey registerSelectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
33+
System.out.println("Register to selector, selectionKey is OP_ACCEPT, return selectionKey = " + registerSelectionKey + ", 注册后selectionKey 数量 = " + selector.keys().size());
34+
35+
// 循环等待客户端连接
36+
while (true) {
37+
// 这里我们等待1秒, 如果没有事件发生,返回
38+
if (selector.select(1000) == 0) {
39+
// 没有事件发生
40+
System.out.println("服务器等待了1秒,无连接");
41+
continue;
42+
}
43+
44+
// 如果返回结果 > 0, 就获取到相关的selectionKey集合
45+
// 1. 如果返回 > 0,表示已经获取到关注的事件
46+
// 2. selector.selectedKeys() 返回关注事件的集合
47+
// 通过selectionKeys反向获取通道
48+
Set<SelectionKey> selectionKeys = selector.selectedKeys();
49+
50+
// 通过迭代器遍历Set<SelectionKey>
51+
Iterator<SelectionKey> keyIterator = selectionKeys.iterator();
52+
53+
while (keyIterator.hasNext()) {
54+
// 获取Selectionkey
55+
SelectionKey key = keyIterator.next();
56+
// 根据key对应的通道发生的事件做响应处理
57+
if (key.isAcceptable()) {
58+
// 如果key是OP_ACCEPT,有新的客户端连接
59+
// 通过ServerSocketChannel生成一个socketChannel
60+
SocketChannel socketChannel = serverSocketChannel.accept();
61+
62+
System.out.println("Register to selector, selectionKey is OP_ACCEPT, return selectionKey = " + registerSelectionKey + ", 注册后selectionKey 数量 = " + selector.keys().size());
63+
64+
// 将SocketChannel设置为非阻塞
65+
socketChannel.configureBlocking(false);
66+
67+
// 将socketChannel注册到selector,关注事件为OP_READ,同时给socketChannel关联一个buffer
68+
socketChannel.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
69+
70+
}
71+
72+
System.out.println("isReadable = " + key.isReadable() + ", isWritable = " + key.isWritable() + ", isValid = " + key.isValid());
73+
74+
if (key.isReadable()) {
75+
// 如果key是OP_READ
76+
// 通过key反向获取到对应channel
77+
SocketChannel channel = (SocketChannel) key.channel();
78+
79+
// 获取该channel关联的buffer
80+
ByteBuffer buffer = (ByteBuffer)key.attachment();
81+
82+
// 从buffer中读取数据到channel中
83+
channel.read(buffer);
84+
85+
System.out.println("from 客户端 " + new String(buffer.array()));
86+
}
87+
88+
// 手动从集合中移动当前的selectionKey,防止重复操作
89+
keyIterator.remove();
90+
}
91+
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)