nc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // CR|LF => CRLF for test most protocols
  34. char* str = (char*)buf;
  35. char eol = str[readbytes-1];
  36. if (eol == '\n' || eol == '\r') {
  37. if (readbytes > 1 && str[readbytes-2] == '\r' && eol == '\n') {
  38. // have been CRLF
  39. }
  40. else {
  41. ++readbytes;
  42. str[readbytes - 2] = '\r';
  43. str[readbytes - 1] = '\n';
  44. }
  45. }
  46. hio_write(sockio, buf, readbytes);
  47. }
  48. void on_close(hio_t* io) {
  49. //printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
  50. hio_del(stdinio, HV_READ);
  51. }
  52. void on_connect(hio_t* io) {
  53. //printf("on_connect fd=%d\n", hio_fd(io));
  54. if (verbose) {
  55. char localaddrstr[SOCKADDR_STRLEN] = {0};
  56. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  57. printf("connect connfd=%d [%s] => [%s]\n", hio_fd(io),
  58. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  59. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  60. }
  61. hio_read(io);
  62. }
  63. int main(int argc, char** argv) {
  64. if (argc < 3) {
  65. printf("\
  66. Usage: nc [-ut] host port\n\
  67. Options:\n\
  68. -t Use tcp protocol (default)\n\
  69. -u Use udp protocol\n\
  70. Examples: nc 127.0.0.1 80\n\
  71. nc -u 127.0.0.1 80\n");
  72. return -10;
  73. }
  74. int index = 1;
  75. const char* protocolname;
  76. if (argv[1][0] == '-') {
  77. ++index;
  78. if (argv[1][1] == 't') {
  79. protocol = 1;
  80. protocolname = "tcp";
  81. }
  82. else if (argv[1][1] == 'u') {
  83. protocol = 2;
  84. protocolname = "udp";
  85. }
  86. }
  87. const char* host = argv[index++];
  88. int port = atoi(argv[index++]);
  89. if (verbose) {
  90. printf("%s %s %d\n", protocolname, host, port);
  91. }
  92. HV_MEMCHECK;
  93. hloop_t* loop = hloop_new(HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS);
  94. // stdin
  95. stdinio = hread(loop, 0, recvbuf, RECV_BUFSIZE, on_stdin);
  96. if (stdinio == NULL) {
  97. return -20;
  98. }
  99. // socket
  100. if (protocol == 1) {
  101. // tcp
  102. sockio = hloop_create_tcp_client(loop, host, port, on_connect);
  103. }
  104. else if (protocol == 2) {
  105. // udp
  106. sockio = hloop_create_udp_client(loop, host, port);
  107. hio_read(sockio);
  108. }
  109. if (sockio == NULL) {
  110. return -20;
  111. }
  112. //printf("sockfd=%d\n", hio_fd(sockio));
  113. hio_setcb_close(sockio, on_close);
  114. hio_setcb_read(sockio, on_recv);
  115. hio_set_readbuf(sockio, recvbuf, RECV_BUFSIZE);
  116. // hio_set_heartbeat(sockio, 1000, send_heartbeat);
  117. hloop_run(loop);
  118. hloop_free(&loop);
  119. return 0;
  120. }