Forum: Ruby Choosing target network for UDP broadcast

Posted by Ivo Wever (ivo)
on 2012-02-03 11:36
If I send a UDP packet containing 'foo' like this:

  socket = UDPSocket.new
  socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
  socket.send('foo', 0, '<broadcast>', 40001)

then wireshark tells me it gets sent from 192.168.x.y. However, my
server is listening on localhost:40001. How can I choose the source
address from which (and the interface via which) the socket will send
its packet?
Posted by Robert Klemme (robert_k78)
on 2012-02-03 13:14
(Received via mailing list)
On Fri, Feb 3, 2012 at 11:36 AM, Ivo Wever <wever@axini.com> wrote:
> If I send a UDP packet containing 'foo' like this:
>
> socket = UDPSocket.new
> socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
> socket.send('foo', 0, '<broadcast>', 40001)
>
> then wireshark tells me it gets sent from 192.168.x.y. However, my
> server is listening on localhost:40001. How can I choose the source
> address from which (and the interface via which) the socket will send
> its packet?

Hm, no UDP specialist here but can't you bind to the proper address
and then send the broadcast?

Kind regards

robert
Posted by Sean O'halpin (sean)
on 2012-02-03 14:48
(Received via mailing list)
On Fri, Feb 3, 2012 at 10:36 AM, Ivo Wever <wever@axini.com> wrote:
> If I send a UDP packet containing 'foo' like this:
>
> socket = UDPSocket.new
> socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
> socket.send('foo', 0, '<broadcast>', 40001)
>
> then wireshark tells me it gets sent from 192.168.x.y. However, my
> server is listening on localhost:40001. How can I choose the source
> address from which (and the interface via which) the socket will send
> its packet?

Use #bind - 
http://ruby-doc.org/stdlib-1.9.3/libdoc/socket/rdoc/Socket.html#method-i-bind
Posted by Sean O'halpin (sean)
on 2012-02-03 14:49
(Received via mailing list)
On Fri, Feb 3, 2012 at 1:47 PM, Sean O'Halpin <sean.ohalpin@gmail.com> 
wrote:
>> its packet?
>
> Use #bind - 
http://ruby-doc.org/stdlib-1.9.3/libdoc/socket/rdoc/Socket.html#method-i-bind

Oops. Wrong link. This is the right one:
http://ruby-doc.org/stdlib-1.9.3/libdoc/socket/rdoc/UDPSocket.html#method-i-bind
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.