<html><body><div dir="ltr"><div>
</div><div><div>
<div>The non-blocking refers only to socket operations, resolving the address is logically outside (it helps to think about C API I guess where the connect gets a resolved address). If you want to async resolve the address you would have to do that separetly (or do it in its own thread since async resolvers are tricky)</div>
<div id="ms-outlook-mobile-signature"><div><br></div><div dir="ltr">Gruss</div><div dir="ltr">Bernd</div><div style="direction:ltr">-- </div><div style="direction:ltr">http://bernd.eckenfels.net</div></div>
</div>
<div> </div><hr style="display:inline-block;width:98%" tabindex="-1"><div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif"><b>Von:</b> nio-dev <nio-dev-retn@openjdk.org> im Auftrag von Didac Vega Bru <dvb.soln@gmail.com><br><b>Gesendet:</b> Montag, September 18, 2023 10:37 PM<br><b>An:</b> nio-dev@openjdk.org <nio-dev@openjdk.org><br><b>Betreff:</b> SocketChannel connect blocks in non-blocking mode<div> </div></font></div>Hello,
<br>
<br>I typically use SocketChannel using blocking mode but lately I was
<br>trying to use non-blocking operations. The issue I find is that,
<br>according to the javadoc for the connect method of SocketChannel, I
<br>would expect the method to return immediately either with a true, false
<br>or fail fast.
<br>
<br>SO, trying a piece of code like the following:
<br>
<br> var socket = SocketChannel.open();
<br> socket.configureBlocking(false);
<br> socket.connect(new InetSocketAddress("non.existing.address", 1111));
<br>
<br>It turns out that the method does not return immediately but blocks
<br>until it throws (the expected) UnresolvedAddressException. This can take
<br>some time, specially if DNS is badly configured.
<br>
<br>| Exception java.nio.channels.UnresolvedAddressException
<br>| at Net.checkAddress (Net.java:149)
<br>| at Net.checkAddress (Net.java:157)
<br>| at SocketChannelImpl.checkRemote (SocketChannelImpl.java:816)
<br>| at SocketChannelImpl.connect (SocketChannelImpl.java:839)
<br>| at (#4:1)
<br>
<br>Looking at the code for the connect method in SocketChannelImpl, one can
<br>find that the first statement of the method is
<br>
<br> @Override
<br> public boolean connect(SocketAddress remote) throws IOException {
<br> SocketAddress sa = checkRemote(remote);
<br> try {
<br> ...
<br>
<br>So, effectively, the address name resolution is performed always blocking.
<br>
<br>Now the question is, I would expect that in non-blocking mode always
<br>return immediately, and catch all non-immediate possible exceptions
<br>later with a call to finishConnect (which name resolution would be part
<br>of). Therefore is this like this for some technical reason? And in that
<br>case shouldn't the javadoc explicitly point this out?
<br>
<br>Thank you,
<br>
<br>Didac V.
<br>
<br></div></div></body></html>