I discovered a few interesting things.
- You can set a property in Java that makes it prefer IPv4-over-IPv6 addresses to simple IPv4 address by setting this property to true: java.net.preferIPv6Addresses. Doing so causes the output to be somewhat different:
~/NetBeansProjects/jruby ➔ jruby -rsocket -e "p TCPServer.gethostbyname('localhost')"
["127.0.0.1", , 2, "127.0.0.1"]
~/NetBeansProjects/jruby ➔ jruby -rsocket -J-Djava.net.preferIPv6Addresses=true -e "p TCPServer.gethostbyname('localhost')"
["0:0:0:0:0:0:0:1", , 30, "0:0:0:0:0:0:0:1"]
Which is closer to what we want, but not quite (note the 30 = IPV6, due to a change I just made to be smarter about IPv6 versus IPv4).
- Most Java socket and net-related classes have somewhat hidden IPv6 versions that might be more appropriate to use in many cases. We need more research to determine if the IPv6 methods would be more appropriate than the default ones we're using (which presumably default to simple IPv4).
This bug does explain one reason why they end up choosing different addresses and why you can have an MRI mongrel and a JRuby mongrel running on the same machine without them conflicting. We need to find a way to get JRuby using the same protocols in the same situations as MRI.