非阻塞套接字的android socket.connect()中未捕获java.net.socketexception networkunreachable异常

非阻塞套接字的android socket.connect()中未捕获java.net.socketexception networkunreachable异常,java,android,sockets,tcp,Java,Android,Sockets,Tcp,我在android中有一个非阻塞tcp客户端。当我尝试连接到不在我的子网中的服务器时,我收到连接超时异常,该异常工作正常,因为它将向目标发送SYN数据包5次tcp_SYN_重试,并返回连接超时错误 现在,当我关闭手机中的WiFi时,手机中只有环回接口,busybox route命令将不显示任何内容。socket.connect中未捕获网络不可访问异常,并返回false 此外,当我注册OP_CONNECT时,会引发CONNECT事件,sock.isConnectionPending和socketC

我在android中有一个非阻塞tcp客户端。当我尝试连接到不在我的子网中的服务器时,我收到连接超时异常,该异常工作正常,因为它将向目标发送SYN数据包5次tcp_SYN_重试,并返回连接超时错误

现在,当我关闭手机中的WiFi时,手机中只有环回接口,busybox route命令将不显示任何内容。socket.connect中未捕获网络不可访问异常,并返回false

此外,当我注册OP_CONNECT时,会引发CONNECT事件,sock.isConnectionPending和socketChannel.finishConnect也会返回true,表示连接已成功建立,尽管没有到服务器的路由

当我在Ubuntu12.04的java编译器中尝试相同的代码时,socket.connect中捕获了非阻塞套接字的异常network unreachable

在android和java编译器中,一切都可以很好地阻止客户端套接字。android和java编译器都捕获到网络不可访问异常

  this.server_ip = "192.168.1.2";
  this.port = 7000;
  try
  {
    // Create a new selector  
    this.selector = SelectorProvider.provider().openSelector();
    InetSocketAddress address = new InetSocketAddress(this.server_ip, this.port);
    this.clientChannel = SocketChannel.open();

    // Configure it as non blocking 
    this.clientChannel.configureBlocking(false);
    Boolean client_status;
    try
    {
      client_status = this.clientChannel.connect(address);
    }
   catch (Exception e) 
   {
     System.out.println(e);
     System.out.println("exception caught in connect");
     return;
   } 
    System.out.println("Client_Connect status "+client_status);
    this.clientChannel.register(this.selector, SelectionKey.OP_CONNECT);
  } 
  catch (Exception e) 
  {
    System.out.println(e);
    System.out.println("exception caught");
    return;
  } 

  while (true)
  {
    try
    { 
      //block for event 
      channels = this.selector.select();
      System.out.println("out of in select");
      // Iterate over the set of keys for which events are available
      Iterator<SelectionKey> selectedKeys = this.selector.selectedKeys().iterator();
      while (selectedKeys.hasNext()) 
      {
        SelectionKey key = (SelectionKey) selectedKeys.next();
        selectedKeys.remove();
        if (!key.isValid()) 
        {
          continue;
        }

        if (key.isReadable()) 
        {
          this.read(key);
        } 
        else if (key.isWritable()) 
        {
          this.write(key);
        }
        else if(key.isConnectable())
        {
          System.out.println("connect ready");

          SocketChannel sock = (SocketChannel )key.channel();
          if(sock.isConnectionPending())
          {
            try
            {
              while (!sock.finishConnect())
              {
                System.out.println("Connection in progress..");
              }
              System.out.println("Connected successfully");
              sock.register(this.selector, SelectionKey.OP_READ);

            } 
            catch(Exception e)
            {
              System.out.println("exception disconnected "+e);
              key.cancel();
              this.selector.wakeup();
            }
          }
        }
      }
    } 
    catch (Exception e) 
    {
      e.printStackTrace();
    }
  }

我错过什么了吗?有人能帮我吗

如果finishConnect返回false,您应该返回到另一个OP_连接的select循环,而不仅仅是占用CPU。@EJP接受了该注释。但我的问题是,当安卓设备中没有到主机的路由时,我并没有弄错。