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)

    The Iroquois had invented 'rifled' arrowhead long before they found themselves face to face with or in possession of rifled fire arms. (62) It does…

  • V for Vendetta

    I went to Wikipedia to look up the Gunpowder Plot and found this: The word 'guy' came thus in the 19th century to mean a weirdly dressed person,…

  • (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…

  • 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.