0

When I read the documentation for SocketChannel, it seems pretty clear that a blocking SocketChannel connect() call will only ever return true or throw an exception. In other words, it can only return false in non-blocking mode. Is that correct, or am I missing/misreading something?

Is it possible for channel.configureBlocking(true) to return and the channel not be in blocking mode? I would expect that if configureBlocking(true) were not able to successfully put the channel in blocking mode (before the return of the method call), an exception would be thrown. Is that correct?

Finally, is there any way for the following code to fail to connect and yet return TRUE? (The code only tests whether the connection succeeds or not, it doesn't do anything with the channel, hence the immediate close):

SocketChannel channel = null;
try {
    channel = SocketChannel.open();
    channel.configureBlocking(true);
    channel.connect(new InetSocketAddress(addr, port));
    return Boolean.TRUE;
}
catch (Exception e) {
    return Boolean.FALSE;
}
finally {
    if (channel != null) {
        try { channel.close() } catch (Exception e) {}
    }
}

Thanks!

batkins
  • 11
  • 2
  • All your assumptions in the question are correct. The code will either connect, or return `FALSE`. What is your problem, though ? – kiruwka Nov 07 '13 at 15:07
  • @kiruwka That's not what he said, and it's not correct. It will either connect and return TRUE, or throw an exception. His problem is rather clearly stated. – user207421 Jul 19 '19 at 07:13

1 Answers1

0
  1. The Javadoc clearly states "If this channel is in blocking mode then an invocation of this method will block until the connection is established or an I/O error occurs". So it either returns true or throws an exception.

  2. 'Is it possible for channel.configureBlocking(true) to return and the channel not be in blocking mode?' No. It will throw an exception if it can't perform the operation. This is also clearly stated in the Javadoc.

user207421
  • 305,947
  • 44
  • 307
  • 483