mcsend.lua 692 B

1234567891011121314151617181920
  1. local socket = require"socket"
  2. local group = "225.0.0.37"
  3. local port = 12345
  4. local c = assert(socket.udp())
  5. --print(assert(c:setoption("reuseport", true)))
  6. --print(assert(c:setsockname("*", port)))
  7. --print(assert(c:setoption("ip-multicast-loop", false)))
  8. --print(assert(c:setoption("ip-multicast-ttl", 4)))
  9. --print(assert(c:setoption("ip-multicast-if", "10.0.1.3")))
  10. --print(assert(c:setoption("ip-add-membership", {multiaddr = group, interface = "*"})))
  11. local i = 0
  12. while 1 do
  13. local message = string.format("hello all %d!", i)
  14. assert(c:sendto(message, group, port))
  15. print("sent " .. message)
  16. socket.sleep(1)
  17. c:settimeout(0.5)
  18. print(c:receivefrom())
  19. i = i + 1
  20. end