Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SelectionKey.isAcceptable()返回;“真的”;当没有传入连接时_Java_Sockets_Nio - Fatal编程技术网

Java SelectionKey.isAcceptable()返回;“真的”;当没有传入连接时

Java SelectionKey.isAcceptable()返回;“真的”;当没有传入连接时,java,sockets,nio,Java,Sockets,Nio,我正在使用JavaNIO用Java编写一个聊天服务器。服务器接受连接时不会出现问题,但在第一个客户端之后,每当select()返回>0时,即使没有挂起的连接,服务器套接字也始终位于所选密钥集中。即使select()返回1,所选密钥集也将有2个元素并包含服务器套接字。这将导致accept()返回null 任何帮助都将不胜感激 主回路: public void start() throws IOException { Set<SelectionKey> keys;

我正在使用JavaNIO用Java编写一个聊天服务器。服务器接受连接时不会出现问题,但在第一个客户端之后,每当select()返回>0时,即使没有挂起的连接,服务器套接字也始终位于所选密钥集中。即使select()返回1,所选密钥集也将有2个元素并包含服务器套接字。这将导致accept()返回null

任何帮助都将不胜感激

主回路:

public void start() throws IOException {
    Set<SelectionKey>       keys;
    Iterator<SelectionKey>  keyIterator;
    this.keepGoing = true;

    while (keepGoing) {
        int readyChannels = this.selector.select();

        if (readyChannels == 0) 
        {
            continue;
        }

        keys        = this.selector.selectedKeys();
        keyIterator = keys.iterator();

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

            if (currentKey.isAcceptable())
            {
                addClient(currentKey);
            }
            if (currentKey.isReadable())
            {
                readSock(currentKey);
            }
            if (currentKey.isWritable())
            {
                // write data to the buffer and remove OP_WRITE
            }
        }
    }

}

必须在keyIterator.next()之后调用keyIterator.remove(),或清除循环末尾的选定键集。选择器不会从该集合中删除键,这取决于您。但您还需要注意,accept()可以在非阻塞模式下返回null,并相应地进行防御编程。

您必须在keyinterator.next()之后调用keyinterator.remove(),或者在循环结束时清除所选的键集。选择器不会从该集合中删除键,这取决于您。但您还需要注意,accept()可以在非阻塞模式下返回null,并相应地进行防御编程。

您必须在keyinterator.next()之后调用keyinterator.remove(),或者在循环结束时清除所选的键集。选择器不会从该集合中删除键,这取决于您。但您还需要注意,accept()可以在非阻塞模式下返回null,并相应地进行防御编程。

您必须在keyinterator.next()之后调用keyinterator.remove(),或者在循环结束时清除所选的键集。选择器不会从该集合中删除键,这取决于您。但您还需要注意,accept()可以在非阻塞模式下返回null,并相应地进行防御编程。

如果您将这些代码示例合并到一个简单的程序中来演示您的问题,这可能会有所帮助。目前,人们需要做大量的工作来在他们的机器上演示您的问题。如果您将这些代码示例组合成一个简单的程序来演示您的问题,这可能会有所帮助。目前,人们需要做大量的工作来在他们的机器上演示您的问题。如果您将这些代码示例组合成一个简单的程序来演示您的问题,这可能会有所帮助。目前,人们需要做大量的工作来在他们的机器上演示您的问题。如果您将这些代码示例组合成一个简单的程序来演示您的问题,这可能会有所帮助。目前,人们需要做大量的工作来在他们的机器上演示您的问题。谢谢,成功了!我在Selector和SelectionKey中查找某种类型的remove(),以为每次都会重新创建该集。谢谢,成功了!我在Selector和SelectionKey中查找某种类型的remove(),以为每次都会重新创建该集。谢谢,成功了!我在Selector和SelectionKey中查找某种类型的remove(),以为每次都会重新创建该集。谢谢,成功了!我在Selector和SelectionKey中查找某种类型的remove(),认为每次都会重新创建该集。
public Server(int port) {

    this.listenPort = port;

    try
    {
        this.selector = Selector.open();
        this.listenChannel = ServerSocketChannel.open();
        this.listenChannel.socket().bind(new InetSocketAddress(this.listenPort), BACKLOG);
        this.listenChannel.configureBlocking(false);
        this.listenChannel.register(this.selector, SelectionKey.OP_ACCEPT);
    } 
    catch (IOException e)
    {
        System.out.println("Server could not initialise: " + e.getMessage());
    }

    this.users = new HashMap<>();
}
private void addClient(SelectionKey key) throws IOException {
    ServerSocketChannel acceptSocket    = (ServerSocketChannel) key.channel();
    SocketChannel       newClient       = acceptSocket.accept();
    SelectionKey        clientKey;

    // Set the new client to non-blocking mode and add to the selector
    newClient.configureBlocking(false);
    clientKey = newClient.register(this.selector, SelectionKey.OP_READ);

    // Add a new key-user pair to the user list
    this.users.put(clientKey, new User());

    // Attach a buffer for reading the packets
    clientKey.attach(new PacketBuffer(newClient));
}