通过 channel 从客户端读取内容时出现 java.nio.channels.IllegalBlockingModeException

标签 java sockets networking nio socketchannel

我对 NIO 很陌生,正在构建一个聊天应用程序,我在所有客户端中都建立了连接,但在从客户端读取内容时,我得到了 java.nio.channels.IllegalBlockingModeException。请帮助我这是发生异常的代码。当到达 PrintRequest 类while (rbc.read(b) != -1) 时发生异常

public class PrintRequest extends Thread
{

    public  PrintRequest(SocketChannel sc,int i)throws Exception
    {
        System.out.println("going to enter the try block of PrintRequest");        
        try
        {   
               System.out.println("Am in the try block of PrintRequest");    

               ReadableByteChannel rbc = Channels.newChannel(sc.socket().getInputStream()); 
               System.out.println("checking in PrintRequest 0001");
               WritableByteChannel wbc = Channels.newChannel(System.out); 
               System.out.println("checking in PrintRequest 0010");
               ByteBuffer b = ByteBuffer.allocateDirect(1024); // read 1024 bytes 
                // int numBytesRead = sc.read(b);
                 System.out.println("checking in PrintRequest 0011");
                 while (rbc.read(b) != -1) 
                 {
                     System.out.println("Am  in while loop of PrintRequest ");
                    b.flip();
                    while (b.hasRemaining())
                    { 
                         wbc.write(b);
                         System.out.println();
                    }
                    b.clear();
                 }

        }
        catch(Exception E)
        {
            System.out.println("Exception in printlnRequest  "+E);
        }                          
    }  }

我的服务器代码:

public class Server
{

  private Selector selector;

 private ServerSocketChannel channel;

  public void listner()
   {

           try
    {
      this.selector = Selector.open();
      this.channel = ServerSocketChannel.open();
      this.channel.configureBlocking(false);
      this.channel.socket().bind(new InetSocketAddress(8888));
      this.channel.register(this.selector, 16);
    }
    catch (IOException e)
    {
      throw new RuntimeException("Could not register listener", e);
    }}


  public void non_Socket()throws Exception
    {

        try
        {
             int i=0;
             this.channel = ServerSocketChannel.open();
             this.channel.configureBlocking(false);
             this.channel.socket().bind(new InetSocketAddress(80));

            while(true)
            {

                this.selector = Selector.open();
                this.channel.register(this.selector, 16);

                Set<SelectionKey> keys = this.selector.selectedKeys();
                Iterator<SelectionKey> iterator = keys.iterator();
                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()) 
                     {
                          // a connection was accepted by a ServerSocketChannel.
                          System.out.println("a connection was accepted by a ServerSocketChannel");
                          SocketChannel sc = this.channel.accept();
                          sc.configureBlocking(false);
                          System.out.println("Received an incoming connection from " + sc.socket().getRemoteSocketAddress()); 
                          System.out.println("checking 0101");
                          // new PrintRequest(sc,i).start(); 
                          System.out.println("checking 0110");

                          if(sc == null )
                            {
                                System.out.println("Please login");
                                Thread.sleep(6000);
                            }
                          else
                          {
                                System.out.println("Last Login was successful");
                               // new PrintRequest(sc,i).start(); 
                                PrintRequest pr=new PrintRequest(sc,i);
                                new Thread(pr).start();

                          }


                     }
                     else if (key.isConnectable())
                     {
                             // a connection was established with a remote server.
                         System.out.println("a connection was established with a remote server");


                     }
                     else if (key.isWritable()) 
                     {
                        // a channel is ready for writing
                         System.out.println(" a channel is ready for writing");
                     }

                     else if (key.isReadable()) 
                     {
                        // a channel is ready for reading
                         System.out.println(" a channel is ready for Reading");
                     } 
                     System.out.println(" a channel is prepare for Reading");
                     keyIterator.remove();
                     Thread.sleep(5000);
                }

           }
        }

        catch(Exception E)
        {
            System.out.println(" Here    : "+E);
        }
        finally
              { 
                   if (channel != null) 
                   { 
                    try 
                    { 
                            channel.close(); 
                    }
                    catch (Exception e) 
                    { 
                            e.printStackTrace(); 
                    }
                   }
               }
    }


    public static void main(String [] abc) throws Exception
    {
        new Server().non_Socket();
    }}

客户端:

public class Client 
{

    public void non_Client_Socket()
    {
        SocketChannel sChannel=null;
        try
        {
            sChannel = SocketChannel.open();
            sChannel.connect(new InetSocketAddress("localhost", 80));
            while (!sChannel.finishConnect())
            {
                System.out.println("Channel is not connected yet");
                Thread.sleep(5000);
            }

            System.out.println("Channel is ready to use");

            /* ----------  going to send data to server ------------*/   
            System.out.println("please enter the text");
            BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
            while(true)
                {
                    System.out.println("Enter the text");
                    String HELLO_REQUEST =stdin.readLine().toString();
                    if(HELLO_REQUEST.equalsIgnoreCase("end"))
                    {
                        break;
                    }

                    System.out.println("Sending a request to HelloServer");    
                    ByteBuffer buffer = ByteBuffer.wrap(HELLO_REQUEST.getBytes());    
                    sChannel.write(buffer); 
                }  
        }
        catch(Exception E)
        {

        }

         finally
        {       
            if (sChannel != null)
            {            
                try 
                {             
                   sChannel.close();            
                }
                catch (Exception e)
                {           
                    e.printStackTrace();       
                }       
            } 

        }  } 
         /* ----------  the data is written in sChannel server will read from this channel  ------------   */




    public static void main(String [] args)throws Exception
    {
        new Client().non_Client_Socket();
    }}

最佳答案

ReadableByteChannel rbc = Channels.newChannel(sc.socket().getInputStream()); 

你的问题就在这里。您无法使用来自非阻塞模式 channel 的流。无论如何,代码毫无意义,因为 sc 已经是一个 ReadableByteChannel。只需删除此行并使用 sc 而不是 rbc 执行以下 I/O。但请参阅下文。

您仍然没有解决我在 previous post 中提到的任何问题。您的连接技术仍然是错误的,并且您正在调用 PrintRequest() ,它以非阻塞模式循环读取,而不是注册 OP_READ 接受的 channel 。该代码仍然没有任何意义。

关于通过 channel 从客户端读取内容时出现 java.nio.channels.IllegalBlockingModeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11132206/

相关文章:

java - Tomcat7服务器错误,目录中不存在文件或不是可读目录

Java 程序不会传输大于 64KB 的文件

c++ - 为以太网适配器设置自定义 ip 时无法绑定(bind)套接字

c# - 从数据包中删除 PPPOE 层

java - 尝试使用 WebService 从 Android 应用程序将数据插入 SQL Server

java - 从主类中的接口(interface)调用默认方法

Java 在 `finally` block 后不打印消息

java - getInetAddress() 返回 0.0.0.0

c# - Quickfix/n-没有连接超时事件或找不到主机?

linux - Linux 上的端口聚合