コード例 #1
0
  /**
   * Starts a non-blocking connection attempt.
   *
   * @throws IOException
   */
  public void connect() throws IOException {
    sc = SocketChannel.open();
    // Very important. Set to non-blocking. Otherwise a call
    // to connect will block until the connection attempt fails
    // or succeeds.
    sc.configureBlocking(false);
    sc.connect(remoteAddress);

    // Registers itself to receive the connect event.
    selectorThread.registerChannelLater(
        sc,
        SelectionKey.OP_CONNECT,
        this,
        new CallbackErrorHandler() {
          public void handleError(Exception ex) {
            listener.connectionFailed(Connector.this, ex);
          }
        });
  }