1
0

nc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include "hloop.h"
  15. #include "hbase.h"
  16. #include "hsocket.h"
  17. #define RECV_BUFSIZE 8192
  18. static char recvbuf[RECV_BUFSIZE];
  19. // 1:tcp 2:udp
  20. int protocol = 1;
  21. // for stdin
  22. hio_t* stdinio = NULL;
  23. // for socket
  24. hio_t* sockio = NULL;
  25. int verbose = 0;
  26. void send_heartbeat(hio_t* io) {
  27. static char buf[] = "PING\r\n";
  28. // printf("send_heartbeat %s", buf);
  29. hio_write(io, buf, 6);
  30. }
  31. void on_recv(hio_t* io, void* buf, int readbytes) {
  32. //printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  33. if (verbose) {
  34. char localaddrstr[SOCKADDR_STRLEN] = {0};
  35. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  36. printf("[%s] <=> [%s]\n",
  37. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  38. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  39. }
  40. printf("%.*s", readbytes, (char*)buf);
  41. // test hio_set_readbuf in hread_cb
  42. #if 0
  43. static int total_readbytes = 0;
  44. total_readbytes += readbytes;
  45. if (total_readbytes >= RECV_BUFSIZE) {
  46. total_readbytes = 0;
  47. }
  48. hio_set_readbuf(io, recvbuf + total_readbytes, RECV_BUFSIZE - total_readbytes);
  49. printf("%.*s", total_readbytes, recvbuf);
  50. #endif
  51. fflush(stdout);
  52. }
  53. void on_stdin(hio_t* io, void* buf, int readbytes) {
  54. //printf("on_stdin fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  55. //printf("> %s\n", buf);
  56. char* str = (char*)buf;
  57. // test hio_read_start/hio_read_stop/hio_close/hloop_stop
  58. #if 1
  59. if (strncmp(str, "start", 5) == 0) {
  60. printf("call hio_read_start\n");
  61. hio_read_start(sockio);
  62. return;
  63. }
  64. else if (strncmp(str, "stop", 4) == 0) {
  65. printf("call hio_read_stop\n");
  66. hio_read_stop(sockio);
  67. return;
  68. }
  69. else if (strncmp(str, "close", 5) == 0) {
  70. printf("call hio_close\n");
  71. hio_close(sockio);
  72. return;
  73. }
  74. else if (strncmp(str, "quit", 4) == 0) {
  75. printf("call hloop_stop\n");
  76. hloop_stop(hevent_loop(io));
  77. return;
  78. }
  79. #endif
  80. // CR|LF => CRLF for test most protocols
  81. char eol = str[readbytes-1];
  82. if (eol == '\n' || eol == '\r') {
  83. if (readbytes > 1 && str[readbytes-2] == '\r' && eol == '\n') {
  84. // have been CRLF
  85. }
  86. else {
  87. ++readbytes;
  88. str[readbytes - 2] = '\r';
  89. str[readbytes - 1] = '\n';
  90. }
  91. }
  92. hio_write(sockio, buf, readbytes);
  93. }
  94. void on_close(hio_t* io) {
  95. //printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
  96. hio_del(stdinio, HV_READ);
  97. }
  98. void on_connect(hio_t* io) {
  99. //printf("on_connect fd=%d\n", hio_fd(io));
  100. if (verbose) {
  101. char localaddrstr[SOCKADDR_STRLEN] = {0};
  102. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  103. printf("connect connfd=%d [%s] => [%s]\n", hio_fd(io),
  104. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  105. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  106. }
  107. hio_read_start(io);
  108. // uncomment to test heartbeat
  109. // hio_set_heartbeat(sockio, 3000, send_heartbeat);
  110. }
  111. int main(int argc, char** argv) {
  112. if (argc < 3) {
  113. printf("\
  114. Usage: nc [-ut] host port\n\
  115. Options:\n\
  116. -t Use tcp protocol (default)\n\
  117. -u Use udp protocol\n\
  118. Examples: nc 127.0.0.1 80\n\
  119. nc -u 127.0.0.1 80\n");
  120. return -10;
  121. }
  122. int index = 1;
  123. const char* protocolname;
  124. if (argv[1][0] == '-') {
  125. ++index;
  126. if (argv[1][1] == 't') {
  127. protocol = 1;
  128. protocolname = "tcp";
  129. }
  130. else if (argv[1][1] == 'u') {
  131. protocol = 2;
  132. protocolname = "udp";
  133. }
  134. }
  135. const char* host = argv[index++];
  136. int port = atoi(argv[index++]);
  137. if (verbose) {
  138. printf("%s %s %d\n", protocolname, host, port);
  139. }
  140. HV_MEMCHECK;
  141. hloop_t* loop = hloop_new(HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS);
  142. // stdin use default readbuf
  143. stdinio = hread(loop, 0, NULL, 0, on_stdin);
  144. if (stdinio == NULL) {
  145. return -20;
  146. }
  147. // socket
  148. if (protocol == 1) {
  149. // tcp
  150. sockio = hloop_create_tcp_client(loop, host, port, on_connect);
  151. }
  152. else if (protocol == 2) {
  153. // udp
  154. sockio = hloop_create_udp_client(loop, host, port);
  155. hio_read(sockio);
  156. }
  157. if (sockio == NULL) {
  158. return -20;
  159. }
  160. // printf("sockfd=%d\n", hio_fd(sockio));
  161. hio_setcb_close(sockio, on_close);
  162. hio_setcb_read(sockio, on_recv);
  163. hio_set_readbuf(sockio, recvbuf, RECV_BUFSIZE);
  164. hloop_run(loop);
  165. hloop_free(&loop);
  166. return 0;
  167. }