Is there a way to bind a socket to a specific local interface ina client application?

J

Joshua M. Miller

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?

TIA,

Josh
 
S

Sam Roberts

Quoting (e-mail address removed), 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
 
P

Patrick Hurley

Try binding to 0.0.0.0, if I understand what you are trying to do that
should take care of it.

Patrick
 
S

Sam Roberts

Quoting (e-mail address removed), 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?

Can't remember if I replied to this... use the Socket class.

Cheers,
Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top