What's the proper way to use SocketChannel.finishConnect()?

Discussion in 'Java' started by -, Jul 25, 2005.

  1. -

    - Guest

    What's the proper way to use SocketChannel.finishConnect()?

    I've read the documentation but couldn't comprehend what is written for it.

    I've seen examples where it is placed:

    1a) Right after SocketChannel.connect() (without while loop)

    SocketChannel sc = SocketChannel.open();
    sc.connect(...);
    sc.finishConnect();
    sc.configureBlocking(false);
    sc.register(selector, SelectionKey.OP_READ);

    1b) Right after SocketChannel.connect() (with while loop)

    SocketChannel sc = SocketChannel.open();
    sc.connect(...);
    while (!sc.finishConnect()) {
    }
    sc.configureBlocking(false);
    sc.register(selector, SelectionKey.OP_READ);


    2a) When iterating the Selector.select() values (without while loop;

    SocketChannel sc = SocketChannel.open();
    sc.connect(...);
    sc.configureBlocking(false);
    sc.register(selector, SelectionKey.OP_CONNECT);

    .... {

    if (sc.isConnectionPending()) {
    sc.finishConnect();
    }

    sc.register(selector, SelectionKey.OP_READ);
    }
    ....

    2b) When iterating the Selector.select() values (with while loop)

    SocketChannel sc = SocketChannel.open();
    sc.connect(...);
    sc.configureBlocking(false);
    sc.register(selector, SelectionKey.OP_CONNECT);

    .... {

    if (sc.isConnectionPending()) {
    while (!sc.finishConnect()) {
    }
    }

    sc.register(selector, SelectionKey.OP_READ);
    }
    ....
    -, Jul 25, 2005
    #1
    1. Advertising

  2. -

    Esmond Pitt Guest

    - wrote:
    > What's the proper way to use SocketChannel.finishConnect()?


    [examples snipped]

    None of the above. If you call connect() while in blocking mode you
    don't need finishConnect() at all. If you call it in non-blocking mode
    you can either loop while (!socketChannel.finishConnect()) ; (preferably
    doing something useful) or register for OP_CONNECT and call
    finishConnect() when it fires. If OP_CONNECT fires, finishConnect()
    should return true for that channel. Don't forget to deregister for
    OP_CONNECT when it fires, otherwise it will get mixed up with OP_WRITE.

    SocketChannel.isConnectionPending() only tells you whether you have
    called connect() in non-blocking mode and not yet had a 'true' from
    finishConnect(), and you don't really need to call it in any of the
    scenarios above as you already know that.
    Esmond Pitt, Jul 26, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. xEM
    Replies:
    1
    Views:
    1,666
    xarax
    Apr 27, 2004
  2. Fritz Bayer
    Replies:
    16
    Views:
    10,834
    Esmond Pitt
    Jul 27, 2004
  3. ATC
    Replies:
    3
    Views:
    3,346
    Steve Horsley
    Sep 11, 2004
  4. Replies:
    2
    Views:
    912
  5. H. Wade Minter
    Replies:
    8
    Views:
    274
    Robin
    Apr 25, 2004
Loading...

Share This Page