nc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * network client
  3. *
  4. * @build: make examples
  5. * @server bin/httpd -s restart -d
  6. * @usage: bin/nc 127.0.0.1 8080
  7. * > GET / HTTP/1.1
  8. * > Connection: close
  9. * > [Enter]
  10. * > GET / HTTP/1.1
  11. * > Connection: keep-alive
  12. * > [Enter]
  13. */
  14. /*
  15. * @介绍:nc是unix下著名的网络连接客户端,此示例程序使用libhv实现了nc类似的功能。
  16. *
  17. */
  18. #include "hloop.h"
  19. #include "hbase.h"
  20. #include "hsocket.h"
  21. #include "hssl.h"
  22. /*
  23. * @test ssl_client
  24. * #define TEST_SSL 1
  25. *
  26. * @build ./configure --with-openssl && make clean && make
  27. *
  28. */
  29. #define TEST_SSL 0
  30. #define RECV_BUFSIZE 8192
  31. static char recvbuf[RECV_BUFSIZE];
  32. // 1:tcp 2:udp
  33. int protocol = 1;
  34. // for stdin
  35. hio_t* stdinio = NULL;
  36. // for socket
  37. hio_t* sockio = NULL;
  38. int verbose = 0;
  39. void send_heartbeat(hio_t* io) {
  40. static char buf[] = "PING\r\n";
  41. // printf("send_heartbeat %s", buf);
  42. hio_write(io, buf, 6);
  43. }
  44. void on_recv(hio_t* io, void* buf, int readbytes) {
  45. //printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  46. if (verbose) {
  47. char localaddrstr[SOCKADDR_STRLEN] = {0};
  48. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  49. printf("[%s] <=> [%s]\n",
  50. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  51. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  52. }
  53. printf("%.*s", readbytes, (char*)buf);
  54. // test hio_set_readbuf in hread_cb
  55. #if 0
  56. static int total_readbytes = 0;
  57. total_readbytes += readbytes;
  58. if (total_readbytes >= RECV_BUFSIZE) {
  59. total_readbytes = 0;
  60. }
  61. hio_set_readbuf(io, recvbuf + total_readbytes, RECV_BUFSIZE - total_readbytes);
  62. printf("%.*s", total_readbytes, recvbuf);
  63. #endif
  64. fflush(stdout);
  65. }
  66. void on_stdin(hio_t* io, void* buf, int readbytes) {
  67. //printf("on_stdin fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  68. //printf("> %s\n", buf);
  69. char* str = (char*)buf;
  70. // test hio_read_start/hio_read_stop/hio_close/hloop_stop
  71. #if 1
  72. if (strncmp(str, "start", 5) == 0) {
  73. printf("call hio_read_start\n");
  74. hio_read_start(sockio);
  75. return;
  76. }
  77. else if (strncmp(str, "stop", 4) == 0) {
  78. printf("call hio_read_stop\n");
  79. hio_read_stop(sockio);
  80. return;
  81. }
  82. else if (strncmp(str, "close", 5) == 0) {
  83. printf("call hio_close\n");
  84. hio_close(sockio);
  85. return;
  86. }
  87. else if (strncmp(str, "quit", 4) == 0) {
  88. printf("call hloop_stop\n");
  89. hloop_stop(hevent_loop(io));
  90. return;
  91. }
  92. #endif
  93. // CR|LF => CRLF for test most protocols
  94. char eol = str[readbytes-1];
  95. if (eol == '\n' || eol == '\r') {
  96. if (readbytes > 1 && str[readbytes-2] == '\r' && eol == '\n') {
  97. // have been CRLF
  98. }
  99. else {
  100. ++readbytes;
  101. str[readbytes - 2] = '\r';
  102. str[readbytes - 1] = '\n';
  103. }
  104. }
  105. hio_write(sockio, buf, readbytes);
  106. }
  107. void on_close(hio_t* io) {
  108. //printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
  109. hio_del(stdinio, HV_READ);
  110. }
  111. void on_connect(hio_t* io) {
  112. //printf("on_connect fd=%d\n", hio_fd(io));
  113. if (verbose) {
  114. char localaddrstr[SOCKADDR_STRLEN] = {0};
  115. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  116. printf("connect connfd=%d [%s] => [%s]\n", hio_fd(io),
  117. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  118. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  119. }
  120. hio_read_start(io);
  121. // uncomment to test heartbeat
  122. // hio_set_heartbeat(sockio, 3000, send_heartbeat);
  123. }
  124. int main(int argc, char** argv) {
  125. if (argc < 3) {
  126. printf("\
  127. Usage: nc [-ut] host port\n\
  128. Options:\n\
  129. -t Use tcp protocol (default)\n\
  130. -u Use udp protocol\n\
  131. Examples: nc 127.0.0.1 80\n\
  132. nc -u 127.0.0.1 80\n");
  133. return -10;
  134. }
  135. int index = 1;
  136. const char* protocolname;
  137. if (argv[1][0] == '-') {
  138. ++index;
  139. if (argv[1][1] == 't') {
  140. protocol = 1;
  141. protocolname = "tcp";
  142. }
  143. else if (argv[1][1] == 'u') {
  144. protocol = 2;
  145. protocolname = "udp";
  146. }
  147. }
  148. const char* host = argv[index++];
  149. int port = atoi(argv[index++]);
  150. if (verbose) {
  151. printf("%s %s %d\n", protocolname, host, port);
  152. }
  153. HV_MEMCHECK;
  154. hloop_t* loop = hloop_new(HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS);
  155. // stdin use default readbuf
  156. stdinio = hread(loop, 0, NULL, 0, on_stdin);
  157. if (stdinio == NULL) {
  158. return -20;
  159. }
  160. // socket
  161. if (protocol == 1) {
  162. #if TEST_SSL
  163. // ssl
  164. sockio = hloop_create_ssl_client(loop, host, port, on_connect);
  165. #else
  166. // tcp
  167. sockio = hloop_create_tcp_client(loop, host, port, on_connect);
  168. #endif
  169. }
  170. else if (protocol == 2) {
  171. // udp
  172. sockio = hloop_create_udp_client(loop, host, port);
  173. hio_read(sockio);
  174. }
  175. if (sockio == NULL) {
  176. return -20;
  177. }
  178. // printf("sockfd=%d\n", hio_fd(sockio));
  179. hio_setcb_close(sockio, on_close);
  180. hio_setcb_read(sockio, on_recv);
  181. hio_set_readbuf(sockio, recvbuf, RECV_BUFSIZE);
  182. hloop_run(loop);
  183. hloop_free(&loop);
  184. return 0;
  185. }