ruby udp broadcast simple example

Here’s a simpler version sender  (client):

require 'socket'
addr = ['<broadcast>', 33333]# broadcast address
#addr = ('', 33333) # broadcast address explicitly [might not work ?]
#addr = ['', 33333] # ??
UDPSock =
UDPSock.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
data = 'I sent this'
UDPSock.send(data, 0, addr[0], addr[1])

Receiver (server):

require 'socket'
addr = ['', 33333]  # host, port
BasicSocket.do_not_reverse_lookup = true
# Create socket and bind to address
UDPSock =
UDPSock.bind(addr[0], addr[1])
data, addr = UDPSock.recvfrom(1024) # if this number is too low it will drop the larger packets and never give them to you
puts "From addr: '%s', msg: '%s'" % [addr[0], data]

5 thoughts on “ruby udp broadcast simple example”

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.