1
0

UdpServer_test.cpp 686 B

12345678910111213141516171819202122232425262728293031323334
  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. while (1) hv_sleep(1);
  28. return 0;
  29. }