1
0

UdpServer_test.cpp 951 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * UdpServer_test.cpp
  3. *
  4. * @build
  5. * make libhv && sudo make install
  6. * g++ -std=c++11 UdpServer_test.cpp -o UdpServer_test -I/usr/local/include/hv -lhv -lpthread
  7. *
  8. */
  9. #include "UdpServer.h"
  10. using namespace hv;
  11. int main(int argc, char* argv[]) {
  12. if (argc < 2) {
  13. printf("Usage: %s port\n", argv[0]);
  14. return -10;
  15. }
  16. int port = atoi(argv[1]);
  17. UdpServer srv;
  18. int bindfd = srv.createsocket(port);
  19. if (bindfd < 0) {
  20. return -20;
  21. }
  22. printf("server bind on port %d, bindfd=%d ...\n", port, bindfd);
  23. srv.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
  24. // echo
  25. printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
  26. channel->write(buf);
  27. };
  28. srv.onWriteComplete = [](const SocketChannelPtr& channel, Buffer* buf) {
  29. printf("> %.*s\n", (int)buf->size(), (char*)buf->data());
  30. };
  31. srv.start();
  32. while (1) sleep(1);
  33. return 0;
  34. }