echoclnt.lua 695 B

1234567891011121314151617181920212223
  1. -----------------------------------------------------------------------------
  2. -- UDP sample: echo protocol client
  3. -- LuaSocket sample files
  4. -- Author: Diego Nehab
  5. -----------------------------------------------------------------------------
  6. local socket = require("socket")
  7. host = host or "localhost"
  8. port = port or 7
  9. if arg then
  10. host = arg[1] or host
  11. port = arg[2] or port
  12. end
  13. host = socket.dns.toip(host)
  14. udp = assert(socket.udp())
  15. assert(udp:setpeername(host, port))
  16. print("Using remote host '" ..host.. "' and port " .. port .. "...")
  17. while 1 do
  18. line = io.read()
  19. if not line or line == "" then os.exit() end
  20. assert(udp:send(line))
  21. dgram = assert(udp:receive())
  22. print(dgram)
  23. end