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
Android 对于非阻塞插座通道,附属插座是否阻塞?_Android_Sockets_Io_Nonblocking_Socketchannel - Fatal编程技术网

Android 对于非阻塞插座通道,附属插座是否阻塞?

Android 对于非阻塞插座通道,附属插座是否阻塞?,android,sockets,io,nonblocking,socketchannel,Android,Sockets,Io,Nonblocking,Socketchannel,我正在开发一个Android应用程序,尝试从套接字上的一个线程执行非阻塞写入,同时在另一个线程上执行阻塞读取。我正在浏览SocketCannel文档,试图弄清楚configureBlocking到底做了什么。特别是,如果我有一个非阻塞的SocketChannel,并且我使用SocketChannel.Socket访问附属的套接字,那么该套接字在某种程度上也是非阻塞的吗?还是阻塞 换句话说,我可以通过为非阻塞方向使用非阻塞SocketChannel并为另一个方向使用附属套接字来获得一个阻塞方向和一

我正在开发一个Android应用程序,尝试从套接字上的一个线程执行非阻塞写入,同时在另一个线程上执行阻塞读取。我正在浏览SocketCannel文档,试图弄清楚configureBlocking到底做了什么。特别是,如果我有一个非阻塞的SocketChannel,并且我使用SocketChannel.Socket访问附属的套接字,那么该套接字在某种程度上也是非阻塞的吗?还是阻塞

换句话说,我可以通过为非阻塞方向使用非阻塞SocketChannel并为另一个方向使用附属套接字来获得一个阻塞方向和一个非阻塞方向的效果吗?

如果套接字具有关联的SocketChannel,则无法直接从其输入流读取。你会得到非法的BlockingModeException。看

您可以通过将非阻塞SocketChannels阻塞到和使用或。这些方法通常会阻塞,直到注册的通道就绪或超时过期

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

修改的示例来自:

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
    }
  }
}