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

Discussion in 'Ruby' started by Joshua M. Miller, Mar 24, 2005.

  1. 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
     
    Joshua M. Miller, Mar 24, 2005
    #1
    1. Advertising

  2. Joshua M. Miller

    Sam Roberts Guest

    Re: Is there a way to bind a socket to a specific local interface in a client application?

    Quoting , 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
     
    Sam Roberts, Mar 24, 2005
    #2
    1. Advertising

  3. Re: Is there a way to bind a socket to a specific local interface in a client application?

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

    Patrick


    On Thu, 24 Mar 2005 23:51:26 +0900, Joshua M. Miller <> wrote:
    > 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
    >
    >
     
    Patrick Hurley, Mar 24, 2005
    #3
  4. Joshua M. Miller

    Sam Roberts Guest

    Re: Is there a way to bind a socket to a specific local interface in a client application?

    Quoting , 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
     
    Sam Roberts, Mar 29, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Antypas

    Using JavaMail API ina proxy server?

    John Antypas, Aug 24, 2003, in forum: Java
    Replies:
    1
    Views:
    4,081
    GaryM
    Aug 26, 2003
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Replies:
    1
    Views:
    500
    Lee Fesperman
    Nov 21, 2003
  3. Marc H.
    Replies:
    2
    Views:
    339
    Martin Franklin
    Mar 13, 2005
  4. Vasu
    Replies:
    0
    Views:
    480
  5. ManicQin
    Replies:
    0
    Views:
    297
    ManicQin
    Feb 9, 2009
Loading...

Share This Page