?
-
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);
}
....
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);
}
....