java Selector例子
时间: 2023-10-20 15:05:50 浏览: 66
以下是一个使用 Java Selector 的简单例子:
```java
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.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
public class SelectorExample {
public static void main(String[] args) throws IOException {
// 创建一个 Selector
Selector selector = Selector.open();
// 创建一个 ServerSocketChannel,并绑定到本地端口
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress("localhost", 8000));
// 设置为非阻塞模式
serverSocketChannel.configureBlocking(false);
// 向 Selector 注册 ServerSocketChannel,并指定为 OP_ACCEPT 事件
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
// 阻塞等待就绪的 Channel,最长阻塞时间为 1 秒
if (selector.select(1000) == 0) {
continue;
}
// 获取 Selector 中所有就绪的 Channel 的 SelectionKey 集合
Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator();
// 遍历 SelectionKey 集合处理就绪的 Channel
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
// 若为 OP_ACCEPT 事件,则处理新的连接
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) key.channel();
SocketChannel socketChannel = server.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
System.out.println("新的客户端连接上来了:" + socketChannel.getRemoteAddress());
}
// 若为 OP_READ 事件,则读取客户端发送的数据
if (key.isReadable()) {
SocketChannel socketChannel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int readBytes = socketChannel.read(buffer);
if (readBytes > 0) {
buffer.flip();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
String message = new String(bytes, "UTF-8");
System.out.println("收到客户端发送的消息:" + message);
} else if (readBytes < 0) {
key.cancel();
socketChannel.close();
}
}
// 处理完毕后移除当前 SelectionKey
keyIterator.remove();
}
}
}
}
```
此例子实现了一个简单的服务器,监听本地 8000 端口,使用 Selector 监听连接、读取数据事件。当有新的客户端连接上来时,输出客户端地址;当客户端发送数据时,输出接收到的消息。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)