UdpServer_test.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * UdpServer_test.cpp
  3. *
  4. * @build make evpp
  5. * @server bin/UdpServer_test 1234
  6. * @client bin/UdpClient_test 1234
  7. *
  8. */
  9. #include <iostream>
  10. #include "UdpServer.h"
  11. using namespace hv;
  12. int main(int argc, char* argv[]) {
  13. if (argc < 2) {
  14. printf("Usage: %s port\n", argv[0]);
  15. return -10;
  16. }
  17. int port = atoi(argv[1]);
  18. UdpServer srv;
  19. int bindfd = srv.createsocket(port);
  20. if (bindfd < 0) {
  21. return -20;
  22. }
  23. printf("server bind on port %d, bindfd=%d ...\n", port, bindfd);
  24. srv.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
  25. // echo
  26. printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
  27. channel->write(buf);
  28. };
  29. srv.start();
  30. std::string str;
  31. while (std::getline(std::cin, str)) {
  32. if (str == "close") {
  33. srv.closesocket();
  34. } else if (str == "start") {
  35. srv.start();
  36. } else if (str == "stop") {
  37. srv.stop();
  38. break;
  39. } else {
  40. srv.sendto(str);
  41. }
  42. }
  43. return 0;
  44. }