Eric Hodel (drbrain) wrote,
Eric Hodel

TCPServer and IPv6

Somebody posted to ruby-core about how they had to create two TCPServer instances before they could connect. Since they were running IPv6, the first TCPServer bound to the IPv6 socket, and the second to the IPv4 socket. I imagine their hosts file is incorrect, so they always tried to connect to the IPv4 socket.

Here's the right way to connect to both IPv6 and IPv4 sockets, if your OS supports both:

require 'socket'
res = Socket.getaddrinfo(nil, 1234, Socket::AF_UNSPEC,
                         Socket::SOCK_STREAM, 0, Socket::AI_PASSIVE)
sockets = []

res.each do |ai|
  sockets <<[3], ai[1])


  • (no subject)

    I dreamed I was in a grocery store with my mom reading Seattle Weekly from sometime late at night until 11AM. The Seattle Weekly had an article about…

  • Free Microwave! (and stuff)

    I have a convection oven microwave (this means you could bake a cake in a metal pan in it) I'd like to get rid of. If you or anyone you know would…

  • Tools!

    Today I used several of my tools. My new Collector Vehicle plate arrived, so I retired the 948-MPC plates to my closet and attached the new plate.…

  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.