Gets the broadcast address of an IP address

Discussion in 'Ruby' started by Sai Hl, Feb 28, 2009.

  1. Sai Hl

    Sai Hl Guest

    Hi Ruby communauty. I am trying to broadcast a message using UDPSocket
    on destination port 10000. My code is:

    client = UDPSocket.new
    client.connect Socket.gethostname, 10000
    client.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
    client.send data, 0

    But my packet is not a broadcast, its address is the same as my host:
    192.168.0.101

    In order to do so on the external interface of my system (named en0), I
    had remplaced this:
    client.connect Socket.gethostname, 10000
    by this:
    client.connect "192.168.0.255", 10000

    And now this is working. But is there a way to get automatically the
    "192.168.0.255" address thanks to a Ruby method? I know I can get my IP
    with IPSocket.getaddress(Socket.gethostname) but I don't see any way to
    gets its broadcast address.

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    Sai Hl, Feb 28, 2009
    #1
    1. Advertising

  2. On 28 Feb 2009, at 16:20, Sai Hl wrote:
    > Hi Ruby communauty. I am trying to broadcast a message using
    > UDPSocket
    > on destination port 10000. My code is:
    >
    > client = UDPSocket.new
    > client.connect Socket.gethostname, 10000
    > client.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
    > client.send data, 0
    >
    > But my packet is not a broadcast, its address is the same as my host:
    > 192.168.0.101
    >
    > In order to do so on the external interface of my system (named
    > en0), I
    > had remplaced this:
    > client.connect Socket.gethostname, 10000
    > by this:
    > client.connect "192.168.0.255", 10000
    >
    > And now this is working. But is there a way to get automatically the
    > "192.168.0.255" address thanks to a Ruby method? I know I can get
    > my IP
    > with IPSocket.getaddress(Socket.gethostname) but I don't see any way
    > to
    > gets its broadcast address.
    >
    > Thanks



    The simplest way to do it is to use a regex to change the last term in
    the quad:

    IPSocket.getaddress(Socket.gethostname).gsub!(/.\d{1,3}$/, '.255')


    Ellie

    Eleanor McHugh
    Games With Brains
    http://slides.games-with-brains.net
    ----
    raise ArgumentError unless @reality.responds_to? :reason
     
    Eleanor McHugh, Feb 28, 2009
    #2
    1. Advertising

  3. On Sat, Feb 28, 2009 at 11:12 AM, Eleanor McHugh
    <> wrote:

    >> ... But is there a way to get automatically the
    >> "192.168.0.255" address thanks to a Ruby method? =A0I know I can get my =

    IP
    >> with IPSocket.getaddress(Socket.gethostname) but I don't see any way to
    >> gets its broadcast address.


    > The simplest way to do it is to use a regex to change the last term in th=

    e
    > quad:
    >
    > IPSocket.getaddress(Socket.gethostname).gsub!(/.\d{1,3}$/, '.255')


    Simple but not necessarily correct :)

    <http://learn-networking.com/network-design/how-a-broadcast-address-works>

    FWIW,
    --=20
    Hassan Schroeder ------------------------
     
    Hassan Schroeder, Feb 28, 2009
    #3
  4. On 28 Feb 2009, at 22:49, Hassan Schroeder wrote:
    > On Sat, Feb 28, 2009 at 11:12 AM, Eleanor McHugh
    > <> wrote:
    >
    >>> ... But is there a way to get automatically the
    >>> "192.168.0.255" address thanks to a Ruby method? I know I can get
    >>> my IP
    >>> with IPSocket.getaddress(Socket.gethostname) but I don't see any
    >>> way to
    >>> gets its broadcast address.

    >
    >> The simplest way to do it is to use a regex to change the last term
    >> in the
    >> quad:
    >>
    >> IPSocket.getaddress(Socket.gethostname).gsub!(/.\d{1,3}$/, '.255')

    >
    > Simple but not necessarily correct :)
    >
    > <http://learn-networking.com/network-design/how-a-broadcast-address-works
    > >


    True, but I'm not sure the general case is so easy on the eye ;p

    subnet_mask = [ 255, 255, 255, 0 ]
    IPSocket
    getaddress(Socket.gethostname).split('.').zip(subnet_mask).collect { |
    (i, m)| ((~m) & 255) | i.to_i }.join(".")


    Ellie

    Eleanor McHugh
    Games With Brains
    http://slides.games-with-brains.net
    ----
    raise ArgumentError unless @reality.responds_to? :reason
     
    Eleanor McHugh, Feb 28, 2009
    #4
  5. Eleanor McHugh wrote:
    > On 28 Feb 2009, at 22:49, Hassan Schroeder wrote:
    >>> The simplest way to do it is to use a regex to change the last term
    >>> in the
    >>> quad:
    >>>
    >>> IPSocket.getaddress(Socket.gethostname).gsub!(/.\d{1,3}$/, '.255')

    >>
    >> Simple but not necessarily correct :)
    >>
    >> <http://learn-networking.com/network-design/how-a-broadcast-address-works
    >> >

    >
    > True, but I'm not sure the general case is so easy on the eye ;p


    This is messier than it should be, but:

    require 'ipaddr'
    n = IPAddr.new("192.168.0.101/24")
    p n | (~n.instance_variable_get:)@mask_addr) & IPAddr::IN4MASK)

    Or maybe you could just broadcast to the all-ones address
    (255.255.255.255)
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Mar 1, 2009
    #5
  6. Sai Hl

    Sai Hl Guest

    Brian Candler wrote:
    > This is messier than it should be, but:
    >
    > require 'ipaddr'
    > n = IPAddr.new("192.168.0.101/24")
    > p n | (~n.instance_variable_get:)@mask_addr) & IPAddr::IN4MASK)
    >
    > Or maybe you could just broadcast to the all-ones address
    > (255.255.255.255)


    Yes, I had use the broadcast address ;)
    Finaly it's simple and efficient. But I think I will try your first
    solution after to.

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
     
    Sai Hl, Mar 1, 2009
    #6
    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. AbraKadabra
    Replies:
    2
    Views:
    666
    Gordon Beaton
    Sep 21, 2005
  2. Stefan Mueller
    Replies:
    5
    Views:
    541
    Steven Saunderson
    Jul 10, 2006
  3. John Joyce

    gets gets

    John Joyce, Mar 26, 2007, in forum: Ruby
    Replies:
    2
    Views:
    374
    John Joyce
    Mar 26, 2007
  4. John Joyce

    Return of gets gets

    John Joyce, Apr 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    210
    John Joyce
    Apr 23, 2007
  5. Replies:
    4
    Views:
    119
    Irmen de Jong
    Aug 24, 2013
Loading...

Share This Page