Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > What's the proper way to use SocketChannel.finishConnect()?

Reply
Thread Tools

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

 
 
-
Guest
Posts: n/a
 
      07-25-2005
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);
}
....

 
Reply With Quote
 
 
 
 
Esmond Pitt
Guest
Posts: n/a
 
      07-26-2005
- 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
proper way to initialize/use persistent properties SilentCry ASP .Net Web Controls 0 05-20-2009 04:36 PM
Proper way to use LIKE operator with SqlDataSource and control parameters in declarative markup? ASP .Net 2 08-18-2007 02:07 AM
What is the proper way to use cookies? Eric Layman ASP .Net 1 04-01-2007 10:22 PM
proper way to use popen4 micklee74@hotmail.com Python 0 06-14-2006 03:42 AM
Proper way to use an imported constant under 'use strict'? H. Wade Minter Perl Misc 8 04-25-2004 12:58 AM



Advertisments