【问题标题】:With a non-blocking SocketChannel, is the affiliated Socket blocking?使用非阻塞 SocketChannel,附属的 Socket 是否阻塞?
【发布时间】:2011-10-09 19:41:42
【问题描述】:

我正在开发一个 Android 应用程序,尝试从套接字上的一个线程执行非阻塞写入,同时在另一个线程上执行阻塞读取。我正在查看 SocketChannel 文档并试图弄清楚 configureBlocking 究竟做了什么。具体来说,如果我有一个非阻塞的 SocketChannel,并且我使用 socketChannel.socket() 访问附属的 Socket,那么该 Socket 在某种程度上也是非阻塞的吗?还是阻塞了?

也就是说,非阻塞方向有一个非阻塞的SocketChannel,另一个方向使用附属的Socket,是否可以得到一个阻塞方向和一个非阻塞方向的效果?

【问题讨论】:

    标签: android sockets io nonblocking socketchannel


    【解决方案1】:

    如果Socket 有一个关联的SocketChannel,您不能直接从它的InputStream 中读取。你会得到IllegalBlockingModeException。见here

    您可以通过 registering 将非阻塞 SocketChannel 阻塞到 Selector 并使用 select()select(long timeout)。这些方法通常会阻塞,直到注册的通道准备好(或超时)。

    对于不使用选择器的线程来说,通道仍然是非阻塞的。

    来自here的修改示例:

    Selector selector = Selector.open();
    channel.configureBlocking(false);
    
    // register for OP_READ: you are interested in reading from the channel
    channel.register(selector, SelectionKey.OP_READ);
    
    while (true) {
      int readyChannels = selector.select(); // This one blocks...
    
      // Safety net if the selector awoke by other means
      if (readyChannels == 0) continue;
    
      Set<SelectionKey> selectedKeys = selector.selectedKeys();
      Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
    
      while (keyIterator.hasNext()) {
        SelectionKey key = keyIterator.next();
    
        keyIterator.remove();
    
        if (!key.isValid()) {
          continue;
        } else if (key.isAcceptable()) {
            // a connection was accepted by a ServerSocketChannel.
        } else if (key.isConnectable()) {
            // a connection was established with a remote server.
        } else if (key.isReadable()) {
            // a channel is ready for reading
        } else if (key.isWritable()) {
            // a channel is ready for writing
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-13
      • 2014-10-19
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多