Quoting jmille24 / leo.gov, on Thu, Mar 24, 2005 at 11:51:26PM +0900:
> I am creating an application that I would like to have bind to one of
> several ethernet devices that I have on my machine.  Does anyone know if
> this can be done with Ruby?  An example:
> 
> A normal socket opening in Ruby:
> 
> client1 = TCPSocket.new('192.168.1.5', port)
> 
> ...here I have opened a TCP socket to 192.168.1.5 on a non-specified
> port. Is there a way to specify the local device that corresponds to IP
> address 192.168.1.6 or any other address associated with a local interface?

Not with TCPSocket. Since it does a connect() inside the #new, a local
ephemeral port gets chosen, so its too late to bind() after doing the new.

You can do this with Socket, though, it has APIs that map fairly
directly to the BSD ones:

 Socket.new -> socket(), so try Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
 Socket.bind -> bind() ...

If its not working for you, repost.

All the socket stuff is undocumented, though the Pickaxe book has a
little bit of info. The V2 might be better (of Pickaxe), haven't got a
copy yet, don't know for sure. If you can read C, you should check the
src out from cvs, it is the best way to figure out how socket works,
sorry, though some apis are documented in cvs now, and I've got some
more docs almost ready to submit.

Cheers,
Sam