From: Eric Hodel Date: 2003-08-13T16:29:10-07:00 Subject: Re: Ruby listens IPv6 --8NvZYKFJsRX2Djef Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Scott Thompson (easco@mac.com) wrote: > Unfortunately, my web browser gave me a "could not connect" error. =20 > Hrmmmm... Out of curiosity, which web browser? No doubt when ruby saw "localhost" and decided to go look it up. If in /etc/hosts, ::1 is listed before 127.0.0.1, then it'll bind to an IPv6 socket. If the IPv4 address is first, it'll bind to the IPv4 address. (Its not a good idea to switch this order though) > After trying the "server" on several ports with no success, checking my= =20 > firewall settings etc... and generally poking around I finally had the=20 > wherewithall to run the program and use the "netstat" command to see if= =20 > the port was actually being listened to. >=20 > I found the line... it looks like this: >=20 > tcp6 0 0 localhost.http *.* =20 > LISTEN >=20 > The interesting part is that this is the ONLY port on my whole system=20 > that is marked as using IPv6. Hmmm... That's probably because the other services are not IPv6 enabled/aware. =20 > What I know about IPv6 could probably fill a couple of sentences. They= =20 > go something like this, "IPv6 is the proposed standard for future=20 > Internet communications. Among a host of features interesting to=20 > networking type, one of the things it does is expands the pool of=20 > available addresses that are available to computers in the world". Well, you can scratch 'proposed', but other than that, accurate. > My question is, can someone tell me why my Ruby script is listening on=20 > IPv6? Is it because of the way that Ruby was compiled (I built 1.8 from= =20 > sources and am using that)? Or, do I have to do something in=20 > particular to tell the Ruby socket that I want it to use IPv4? If you want it to use IPv4, use an IPv4 address, "127.0.0.1" will do, you can also get a browser that is IPv6 aware, and it'll just work. (Note that everybody with a valid IPv4 address has an IPv6 address automatically.) > I would also welcome pointers to documentation for neophytes about Ruby= =20 > sockets... though you may want to send them to me off-line so the other= =20 > folks on the list don't have to endure them again. :-) The easiest way to do use sockets is threading: server =3D TCPServer.open(host, port) loop do Thread.start(server.accept) do |s| # one thread per client port =3D s.peeraddr[1] name =3D s.peeraddr[2] addr =3D s.peeraddr[3] puts "*** recieving from #{name}:#{port}" begin puts line while line =3D s.gets ensure s.close end puts "*** done with #{name}:#{port}" end end Each client connection starts a new thread to work with. You don't have to worry about select() because Ruby takes care of it for you in the background. And feel free to ask questions, that's what we're here for. --=20 Eric Hodel - drbrain@segment7.net - http://segment7.net All messages signed with fingerprint: FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --8NvZYKFJsRX2Djef Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (FreeBSD) iD8DBQE/OsnGMypVHHlsnwQRAiDdAJ9O1IVl3UNCFrhW8DR/uAUktfmCIwCg5kwM xsji59XLcwg7XKiUGOb06Vo= =PkaA -----END PGP SIGNATURE----- --8NvZYKFJsRX2Djef--