nc.c 4.1 KB

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