JRuby (please use github issues at http://bugs.jruby.org)
  1. JRuby (please use github issues at http://bugs.jruby.org)
  2. JRUBY-2655

TCPSocket.new('localhost', 2001) connects on the wrong interface

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: JRuby 1.1.2
    • Fix Version/s: JRuby 1.2
    • Component/s: Core Classes/Modules
    • Labels:
      None
    • Environment:
      OS X 10.5.3
    • Number of attachments :
      0

      Description

      Using TCPSocket.new with the 'localhost' address does not use the loopback interface. MRI properly listens/connects on the loopback interface when using 'localhost'.

        Activity

        Hide
        Kevin Ballard added a comment -

        In a subsequent test, I may be wrong about why this is failing. It seems that maybe MRI resolves 'localhost' to ::1 and JRuby resolves it to '127.0.0.1'.

        In any case, when you're using an IPv6-supporting OS (such as OS X) and you run a TCPServer under MRI for 'localhost' and a TCPSocket under JRuby for 'localhost', they can't connect.

        Show
        Kevin Ballard added a comment - In a subsequent test, I may be wrong about why this is failing. It seems that maybe MRI resolves 'localhost' to ::1 and JRuby resolves it to '127.0.0.1'. In any case, when you're using an IPv6-supporting OS (such as OS X) and you run a TCPServer under MRI for 'localhost' and a TCPSocket under JRuby for 'localhost', they can't connect.
        Hide
        Charles Oliver Nutter added a comment -

        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.

        Show
        Charles Oliver Nutter added a comment - 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.
        Hide
        Charles Oliver Nutter added a comment -

        This appears to work fine now on my machine, both connected to a network and otherwise. So for now I'll mark this resolved and if it's still happening on OS X or another platform we can reopen or file a new bug.

        Show
        Charles Oliver Nutter added a comment - This appears to work fine now on my machine, both connected to a network and otherwise. So for now I'll mark this resolved and if it's still happening on OS X or another platform we can reopen or file a new bug.

          People

          • Assignee:
            Charles Oliver Nutter
            Reporter:
            Kevin Ballard
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: