Trying lately to get Jetty to work with Jersey on my computers. getting those “Unable to establish loopback connection” and “Connection refused” errors. Googling this indicates it is a firewall issue but I had to try to debug anyway right..
So I tried to write some simple code to test if I had problems binding on localhost due to firewall. Surely I discovered (in Java) you can do ServerSocket.accept() and bind to a port with no problem. So I thought it had to be something else.
Turns out Jetty uses NIO which is bit different. Where it crashes it the NIO call Selector.open(). This not only tries to bind to a port or whatever, it picks one at random and then tries to connect to it. So just writing another debug program and oneliner is enough, Selector.open() fails allways for me. This is what kills Jetty as well. Turns out it is a firewall issue after all. But it does not show all the time since my laptop is configured by the nice people at the office to have different rules depending on which type of network I am in. So sometimes it blocks the loopback and other times not.
So few things to take home here:
Selector.open() binds on localhost on random port and connects to it. You need both rights to bind and connect to localhost to use Jetty at all. Seems weird to make such assumptions on basic NIO code. But this is what I have to live with.
Sometimes the firewall may block you, sometimes not. Can depend on lots of things like are you on internet, where did you connect from, etc. What is the point in blocking your own localhost from connecting to localhost anyway?
The IPv4 localhost address (127.0.0.1) in IPv6 seems to translate to 7F00. This is at least what the FW logs show me. Just a reminder after I forget it.. And IPv6 localhost address is ::1 (yes three characters). Some software works for me with this, which I guess is due to the loopback being blocked using IPv4 filters.. too bad it does not work for everything. I think it depends on how the SW binds, whether that is done using IPv6 or IPv4 address. The IPv6 version should include the IPv4 address as a subaddress, which might explain why it works for SW that binds using the IPv6 stuff.