1
0

UdpServer_test.cpp 791 B

1234567891011121314151617181920212223242526272829303132333435363738
  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 "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.start();
  29. // press Enter to stop
  30. while (getchar() != '\n');
  31. return 0;
  32. }