Skip to content

Instantly share code, notes, and snippets.

@towynlin
Created December 16, 2013 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save towynlin/7996957 to your computer and use it in GitHub Desktop.
Save towynlin/7996957 to your computer and use it in GitHub Desktop.
ruby listener for Spark Core multicast presence announcement
require 'socket'
require 'ipaddr'
MULTICAST_ADDR = '224.0.1.187'
PORT = 5683
ip = IPAddr.new(MULTICAST_ADDR).hton + IPAddr.new('0.0.0.0').hton
sock = UDPSocket.new
sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, ip)
sock.bind(Socket::INADDR_ANY, PORT)
loop do
msg, info = sock.recvfrom(1024)
id = msg[7, 12].bytes.map { |b| b.to_i.to_s(16) }.join
puts "Spark Core IP: #{info[2]}"
puts " ID: #{id}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment