nc.c 4.4 KB

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