Skip to content

Commit b87e02b

Browse files
committed
ignore
1 parent 21d3858 commit b87e02b

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
import java.net.InetSocketAddress;
3+
import java.net.StandardSocketOptions;
4+
import java.nio.ByteBuffer;
5+
import java.nio.channels.DatagramChannel;
6+
import java.nio.charset.StandardCharsets;
7+
8+
public class NioClient {
9+
public static void main(String[] args) throws Exception{
10+
11+
DatagramChannel datagramChannel = DatagramChannel.open();
12+
datagramChannel.setOption(StandardSocketOptions.SO_REUSEADDR, Boolean.TRUE);
13+
// 绑定发送端口
14+
datagramChannel.bind(new InetSocketAddress(7725));
15+
16+
17+
ByteBuffer byteBuffer = ByteBuffer.allocate(65507);
18+
byteBuffer.put("Nio UDP".getBytes(StandardCharsets.UTF_8));
19+
byteBuffer.flip();
20+
21+
// 返回已经成功发送的字节数据, 可能一次性没发送完毕
22+
int count = datagramChannel.send(byteBuffer, new InetSocketAddress("127.0.0.1", 1024));
23+
24+
System.out.println(count);
25+
}
26+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
----------------------------
2+
selector |
3+
----------------------------
4+
import java.net.InetSocketAddress;
5+
import java.net.StandardSocketOptions;
6+
import java.nio.ByteBuffer;
7+
import java.nio.channels.DatagramChannel;
8+
import java.nio.charset.StandardCharsets;
9+
10+
public class NioUdpServer {
11+
12+
public static void main(String[] args) throws Exception {
13+
14+
try(DatagramChannel datagramChannel = DatagramChannel.open()){
15+
16+
datagramChannel.bind(new InetSocketAddress(1024));
17+
// 一些socket的选项
18+
datagramChannel.setOption(StandardSocketOptions.SO_REUSEADDR, Boolean.TRUE);
19+
20+
// 预定义缓冲区
21+
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(65507);
22+
23+
// 阻塞接收数据
24+
while (true){
25+
// 客户端地址信息
26+
InetSocketAddress inetSocketAddress = (InetSocketAddress) datagramChannel.receive(byteBuffer);
27+
String hostName = inetSocketAddress.getHostName();
28+
int port = inetSocketAddress.getPort();
29+
String hostString = inetSocketAddress.getHostString();
30+
System.out.println(hostName + "[" + hostString +"]:" + port);
31+
32+
// 数据信息
33+
byteBuffer.flip();
34+
byte[] bytes = new byte[byteBuffer.remaining()];
35+
byteBuffer.get(bytes);
36+
byteBuffer.clear();
37+
System.out.println(new String(bytes, StandardCharsets.UTF_8));
38+
}
39+
}
40+
}
41+
}
42+
43+
44+
----------------------------
45+
selector |
46+
----------------------------
47+
# udp的selector只有一个作用, 就是用它来轮询n个客户端/服务端
48+
* udp没有连接, 不需要去监听状态, 也不存在阻塞等待数据的情况
49+
* 如果只是一个客户端/服务端, 那么使用传统的:DatagramSocket 没区别
50+
51+
import java.net.InetSocketAddress;
52+
import java.net.StandardSocketOptions;
53+
import java.nio.ByteBuffer;
54+
import java.nio.channels.DatagramChannel;
55+
import java.nio.channels.SelectionKey;
56+
import java.nio.channels.Selector;
57+
import java.nio.charset.StandardCharsets;
58+
import java.util.Iterator;
59+
60+
public class NioUdpServer {
61+
62+
public static void main(String[] args) throws Exception {
63+
64+
Selector selector = Selector.open();
65+
DatagramChannel datagramChannel = DatagramChannel.open();
66+
67+
try{
68+
datagramChannel.bind(new InetSocketAddress(1024));
69+
70+
// 非阻塞模式
71+
datagramChannel.configureBlocking(false);
72+
// 一些socket的选项
73+
datagramChannel.setOption(StandardSocketOptions.SO_REUSEADDR, Boolean.TRUE);
74+
75+
datagramChannel.register(selector, SelectionKey.OP_READ);
76+
77+
// 预定义缓冲区
78+
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(65507);
79+
80+
while (selector.select() > 0){
81+
Iterator<SelectionKey> selectionKeyIterator = selector.selectedKeys().iterator();
82+
while (selectionKeyIterator.hasNext()){// 读事件
83+
SelectionKey selectionKey = selectionKeyIterator.next();
84+
try{
85+
if(selectionKey.isReadable()){
86+
87+
DatagramChannel channel = (DatagramChannel) selectionKey.channel();
88+
89+
// 客户端地址信息
90+
InetSocketAddress inetSocketAddress = (InetSocketAddress) channel.receive(byteBuffer);
91+
String hostName = inetSocketAddress.getHostName();
92+
int port = inetSocketAddress.getPort();
93+
String hostString = inetSocketAddress.getHostString();
94+
System.out.println(hostName + "[" + hostString +"]:" + port);
95+
96+
// 数据信息
97+
byteBuffer.flip();
98+
byte[] bytes = new byte[byteBuffer.remaining()];
99+
byteBuffer.get(bytes);
100+
byteBuffer.clear();
101+
System.out.println(new String(bytes, StandardCharsets.UTF_8));
102+
103+
}else if(selectionKey.isWritable()){
104+
}else if(selectionKey.isAcceptable()){
105+
}else if(selectionKey.isConnectable()){
106+
}else{ }
107+
}finally {
108+
selectionKeyIterator.remove();
109+
}
110+
}
111+
}
112+
113+
}finally {
114+
selector.close();
115+
datagramChannel.close();
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)