1
0

tcp_echo_server.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * tcp echo server
  3. *
  4. * @build make examples
  5. * @server bin/tcp_echo_server 1234
  6. * @client bin/nc 127.0.0.1 1234
  7. * nc 127.0.0.1 1234
  8. * telnet 127.0.0.1 1234
  9. */
  10. #include "hloop.h"
  11. #include "hsocket.h"
  12. #include "hssl.h"
  13. /*
  14. * @test ssl_server
  15. * #define TEST_SSL 1
  16. *
  17. * @build ./configure --with-openssl && make clean && make
  18. *
  19. */
  20. #define TEST_SSL 0
  21. #define TEST_READ_ONCE 0
  22. #define TEST_READLINE 0
  23. #define TEST_READSTRING 0
  24. #define TEST_READBYTES 0
  25. #define TEST_READ_STOP 0
  26. #define TEST_UNPACK 0
  27. #if TEST_UNPACK
  28. static unpack_setting_t unpack_setting;
  29. #endif
  30. // hloop_create_tcp_server -> on_accept -> hio_read -> on_recv -> hio_write
  31. static void on_close(hio_t* io) {
  32. printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
  33. }
  34. static void on_recv(hio_t* io, void* buf, int readbytes) {
  35. printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  36. char localaddrstr[SOCKADDR_STRLEN] = {0};
  37. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  38. printf("[%s] <=> [%s]\n",
  39. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  40. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  41. printf("< %.*s", readbytes, (char*)buf);
  42. // echo
  43. printf("> %.*s", readbytes, (char*)buf);
  44. hio_write(io, buf, readbytes);
  45. #if TEST_READ_STOP
  46. hio_read_stop(io);
  47. #elif TEST_READ_ONCE
  48. hio_read_once(io);
  49. #elif TEST_READLINE
  50. hio_readline(io);
  51. #elif TEST_READSTRING
  52. hio_readstring(io);
  53. #elif TEST_READBYTES
  54. hio_readbytes(io, TEST_READBYTES);
  55. #endif
  56. }
  57. static void on_accept(hio_t* io) {
  58. printf("on_accept connfd=%d\n", hio_fd(io));
  59. char localaddrstr[SOCKADDR_STRLEN] = {0};
  60. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  61. printf("accept connfd=%d [%s] <= [%s]\n", hio_fd(io),
  62. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  63. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  64. hio_setcb_close(io, on_close);
  65. hio_setcb_read(io, on_recv);
  66. #if TEST_UNPACK
  67. hio_set_unpack(io, &unpack_setting);
  68. #endif
  69. #if TEST_READ_ONCE
  70. hio_read_once(io);
  71. #elif TEST_READLINE
  72. hio_readline(io);
  73. #elif TEST_READSTRING
  74. hio_readstring(io);
  75. #elif TEST_READBYTES
  76. hio_readbytes(io, TEST_READBYTES);
  77. #else
  78. hio_read_start(io);
  79. #endif
  80. }
  81. int main(int argc, char** argv) {
  82. if (argc < 2) {
  83. printf("Usage: %s port|path\n", argv[0]);
  84. return -10;
  85. }
  86. const char* host = "0.0.0.0";
  87. int port = atoi(argv[1]);
  88. #if ENABLE_UDS
  89. if (port == 0) {
  90. host = argv[1];
  91. }
  92. #endif
  93. #if TEST_SSL
  94. hssl_ctx_init_param_t ssl_param;
  95. memset(&ssl_param, 0, sizeof(ssl_param));
  96. ssl_param.crt_file = "cert/server.crt";
  97. ssl_param.key_file = "cert/server.key";
  98. ssl_param.endpoint = HSSL_SERVER;
  99. if (hssl_ctx_init(&ssl_param) == NULL) {
  100. fprintf(stderr, "hssl_ctx_init failed!\n");
  101. return -30;
  102. }
  103. #endif
  104. #if TEST_UNPACK
  105. memset(&unpack_setting, 0, sizeof(unpack_setting_t));
  106. unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
  107. unpack_setting.mode = UNPACK_BY_DELIMITER;
  108. unpack_setting.delimiter[0] = '\r';
  109. unpack_setting.delimiter[1] = '\n';
  110. unpack_setting.delimiter_bytes = 2;
  111. #endif
  112. hloop_t* loop = hloop_new(0);
  113. #if TEST_SSL
  114. hio_t* listenio = hloop_create_ssl_server(loop, host, port, on_accept);
  115. #else
  116. hio_t* listenio = hloop_create_tcp_server(loop, host, port, on_accept);
  117. #endif
  118. if (listenio == NULL) {
  119. return -20;
  120. }
  121. printf("listenfd=%d\n", hio_fd(listenio));
  122. hloop_run(loop);
  123. hloop_free(&loop);
  124. return 0;
  125. }