1
0

UdpServer_test.cpp 718 B

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * UdpServer_test.cpp
  3. *
  4. * @build: make evpp
  5. *
  6. */
  7. #include "UdpServer.h"
  8. using namespace hv;
  9. int main(int argc, char* argv[]) {
  10. if (argc < 2) {
  11. printf("Usage: %s port\n", argv[0]);
  12. return -10;
  13. }
  14. int port = atoi(argv[1]);
  15. UdpServer srv;
  16. int bindfd = srv.createsocket(port);
  17. if (bindfd < 0) {
  18. return -20;
  19. }
  20. printf("server bind on port %d, bindfd=%d ...\n", port, bindfd);
  21. srv.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
  22. // echo
  23. printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
  24. channel->write(buf);
  25. };
  26. srv.start();
  27. // press Enter to stop
  28. while (getchar() != '\n');
  29. return 0;
  30. }