Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.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 爪哇。尼奥。将多个通道连接到一个选择器_Java_Client Server_Selector_Nio_Channel - Fatal编程技术网

Java 爪哇。尼奥。将多个通道连接到一个选择器

Java 爪哇。尼奥。将多个通道连接到一个选择器,java,client-server,selector,nio,channel,Java,Client Server,Selector,Nio,Channel,我试图理解如何用一个选择器和多个通道编写最简单的应用程序 经过难以置信的努力,我能够编写以下代码: 服务器: private static byte[] data = new byte[255]; public static void main(String[] args) throws IOException { for (int i = 0; i < data.length; i++) data[i] = (byte) i;

我试图理解如何用一个选择器和多个通道编写最简单的应用程序

经过难以置信的努力,我能够编写以下代码:

服务器:

private static byte[] data = new byte[255];

    public static void main(String[] args) throws IOException {
        for (int i = 0; i < data.length; i++)
            data[i] = (byte) i;

        ServerSocketChannel server = ServerSocketChannel.open();
        server.configureBlocking(false);

        server.socket().bind(new InetSocketAddress(9000));
        Selector selector = Selector.open();
        server.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            selector.select();
            Set readyKeys = selector.selectedKeys();
            Iterator iterator = readyKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey key = (SelectionKey) iterator.next();
                iterator.remove();
                if (key.isAcceptable()) {
                    SocketChannel client = server.accept();
                    System.out.println("Accepted connection from " + client);
                    client.configureBlocking(false);
                    ByteBuffer source = ByteBuffer.wrap(data);
                    SelectionKey key2 = client.register(selector, SelectionKey.OP_WRITE);
                    key2.attach(source);
                } else if (key.isWritable()) {
                    SocketChannel client = (SocketChannel) key.channel();
                    ByteBuffer output = (ByteBuffer) key.attachment();
                    if (!output.hasRemaining()) {
                        output.rewind();
                    }
                    client.write(output);
                }
                key.channel().close();
            }
        }
    }
我的步骤:

  • 运行服务器
  • 运行客户端
  • 在服务器控制台中执行此步骤后,我看到以下内容:

    Accepted connection from java.nio.channels.SocketChannel[connected local=/127.0.0.1:9000 remote=/127.0.0.1:49184]
    
    Exception in thread "main" java.nio.channels.NotYetConnectedException
        at sun.nio.ch.SocketChannelImpl.ensureWriteOpen(SocketChannelImpl.java:269)
        at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:474)
        at io.nio.SocketSender.main(SocketSender.java:25)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    
    在客户端控制台中,我看到以下内容:

    Accepted connection from java.nio.channels.SocketChannel[connected local=/127.0.0.1:9000 remote=/127.0.0.1:49184]
    
    Exception in thread "main" java.nio.channels.NotYetConnectedException
        at sun.nio.ch.SocketChannelImpl.ensureWriteOpen(SocketChannelImpl.java:269)
        at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:474)
        at io.nio.SocketSender.main(SocketSender.java:25)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    

    请帮助理解堆栈跟踪的原因。

    消息非常清楚。我将以阻塞模式进行客户端连接:然后它要么完成,要么失败。否则,您必须使用选择器,为OP_CONNECT选择,使用finishConnect(),测试其返回值,等等。事实上,我将通过客户端使用阻塞模式,而我可能根本不会使用NIO