test-socket-udp-echo-server.nut 908 B

1234567891011121314151617181920212223242526
  1. ////////////////////////////////////////////////////////////////////////////-
  2. // UDP sample: echo protocol client
  3. // LuaSocket sample files
  4. // Author: Diego Nehab
  5. // RCS ID: $Id: echoclnt.lua,v 1.10 2005/01/02 22:44:00 diego Exp $
  6. ////////////////////////////////////////////////////////////////////////////-
  7. local host = "127.0.0.1"
  8. local port = 20007;
  9. if (vargv.len() > 0){
  10. host = vargv[0] || host;
  11. port = vargv[1] || port;
  12. }
  13. print("Binding to host '" + host + "' and port " + port + "...");
  14. local udp = socket.udp();
  15. udp.setsockname(host, port);
  16. udp.settimeout(5);
  17. local sockname = udp.getsockname();
  18. print("Waiting packets on " + sockname.address + ":" + sockname.port + "...");
  19. while (true){
  20. local dgram = udp.receivefrom();
  21. if (dgram) {
  22. print("Echoing '" + dgram.data + "' to " + dgram.address + ":" + dgram.port);
  23. udp.sendto(dgram.data, dgram.address, dgram.port);
  24. }
  25. else print(ip);
  26. }