Java NIO client

后端 未结 2 789
遇见更好的自我
遇见更好的自我 2020-12-04 01:08
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.By         


        
2条回答
  •  臣服心动
    2020-12-04 01:45

    You need to check for a connectable key, e.g.

    if (key.isConnectable()) {
      this.doConnect(key);
    }
    ...
    private void doConnect(SelectionKey key) {
      SocketChannel channel = (SocketChannel) key.channel();
      if (channel.finishConnect()) {
        /* success */
      } else {
        /* failure */
      }
    }
    

    Use SocketChannel.finishConnect to determine whether the connection was established successfully.

提交回复
热议问题