Skip to content

Instantly share code, notes, and snippets.

@cmol
Last active October 1, 2020 18:17
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 cmol/1656b1780df33e7d435f3ab01b8cbe12 to your computer and use it in GitHub Desktop.
Save cmol/1656b1780df33e7d435f3ab01b8cbe12 to your computer and use it in GitHub Desktop.
def echo_listener(socket, linklocal_addr, multicast_addr, multicast_port,
flags, msg_length)
loop do
# Listen for messages of up to specified length
message, client = socket.recvfrom(msg_length)
# Extract client information given as array and log connection
addr_info = Addrinfo.new(client)
# We are not interested in messages from our selves
next if addr_info.ip_address == linklocal_addr
# Write out the received message
puts message
# Only reply if a request is send. We will make infinite packet loops
# in our network if we don't do this
if(message.split.first == "[REQUEST]")
puts "========= Sending echo REPLY to #{addr_info.ip_address}"
socket.send("[REPLY] Hello from #{addr_info.ip_address}",
flags, multicast_addr, multicast_port)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment