【Android】Java NIO(New I/O)的`Selector`类来实现非阻塞的Socket监听

如果你不想使用循环来监听客户端的连接和数据,你可以使用Java NIO(New I/O)的Selector类来实现非阻塞的Socket监听。Selector类提供了一种选择一组已经就绪的通道的机制,这样你就不需要使用循环来等待连接和数据。

以下是使用Selector类的示例代码:

java 复制代码
private ServerSocketChannel serverSocketChannel;
private Selector selector;

private void startServer() throws IOException {
    serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.configureBlocking(false);
    serverSocketChannel.socket().bind(new InetSocketAddress(8888));

    selector = Selector.open();
    serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

    while (true) {
        int readyChannels = selector.select();
        if (readyChannels == 0) {
            continue;
        }

        Set<SelectionKey> selectedKeys = selector.selectedKeys();
        Iterator<SelectionKey> keyIterator = selectedKeys.iterator();

        while (keyIterator.hasNext()) {
            SelectionKey key = keyIterator.next();

            if (key.isAcceptable()) {
                handleAcceptableKey(key);
            } else if (key.isReadable()) {
                handleReadableKey(key);
            }

            keyIterator.remove();
        }
    }
}

private void handleAcceptableKey(SelectionKey key) throws IOException {
    ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
    SocketChannel clientChannel = serverChannel.accept();
    clientChannel.configureBlocking(false);
    clientChannel.register(selector, SelectionKey.OP_READ);
    Log.d("Server", "设备已连接");
}

private void handleReadableKey(SelectionKey key) throws IOException {
    SocketChannel channel = (SocketChannel) key.channel();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    int bytesRead = channel.read(buffer);

    if (bytesRead == -1) {
        channel.close();
        Log.d("Server", "设备已断开连接");
        return;
    }

    buffer.flip();
    byte[] data = new byte[buffer.limit()];
    buffer.get(data);
    String receivedData = new String(data);
    Log.d("Server", "接收到数据:" + receivedData);

    // 处理接收到的数据

    // 回复客户端
    ByteBuffer responseBuffer = ByteBuffer.wrap(("已接收到数据:" + receivedData).getBytes());
    channel.write(responseBuffer);
}

在上面的代码中,我们首先创建一个ServerSocketChannel对象,并将其配置为非阻塞模式。然后,我们将ServerSocketChannel绑定到指定的端口,并将其注册到Selector中,以便监听客户端连接。

while循环中,我们使用selector.select()方法来选择已经就绪的通道。如果没有就绪的通道,我们可以继续等待。一旦有通道就绪,我们使用selector.selectedKeys()方法获取已选择的键集合,并使用迭代器遍历这些键。

在迭代器循环中,我们根据键的类型判断通道是否可接受连接或可读取数据。如果是可接受连接的通道,我们使用handleAcceptableKey()方法处理。如果是可读取数据的通道,我们使用handleReadableKey()方法处理。

handleAcceptableKey()方法中,我们使用serverChannel.accept()方法来接受客户端连接,并将客户端通道注册到Selector中,以便监听客户端发送的数据。

handleReadableKey()方法中,我们首先读取客户端发送的数据,并将其存储在ByteBuffer中。如果读取到的字节数为-1,表示客户端断开连接,我们关闭通道并在日志中打印出"设备已断开连接"。如果读取到有效的数据,我们可以在代码中处理这些数据,并通过通道向客户端发送回复。

请注意,这只是一个使用Selector的简单示例代码。你需要根据你的实际需求进行适当的修改和优化。

希望这可以解决你的问题!

相关推荐
区区一散修8 分钟前
Java进阶 6. 集合
java·开发语言
chen_mangoo12 分钟前
HDMI简介
android·linux·驱动开发·单片机·嵌入式硬件
阿里-于怀15 分钟前
AgentScope AutoContextMemory:告别 Agent 上下文焦虑
android·java·数据库·agentscope
OpenTiny社区16 分钟前
TinyPro v1.4.0 正式发布:支持 Spring Boot、移动端适配、新增卡片列表和高级表单页面
java·前端·spring boot·后端·开源·opentiny
lonelyhiker17 分钟前
IDEA未编译完就启动项目的问题
java·ide·intellij-idea
月入2k没钱吃饭没得朋友还被迫加班22 分钟前
meterSphere二开遇到的问题
java
aloha_78922 分钟前
langchain4j如何使用mcp
java·人工智能·python·langchain
小希smallxi31 分钟前
Java 程序调用 FFmpeg 教程
java·python·ffmpeg
李慕婉学姐33 分钟前
【开题答辩过程】以《基于Java的周边游优选推荐网站的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·开发语言
Larry_Yanan40 分钟前
Qt安卓开发(三)双摄像头内嵌布局
android·开发语言·c++·qt·ui