client.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "hloop.h"
  2. #include "htime.h"
  3. #include "hsocket.h"
  4. #define RECV_BUFSIZE 8192
  5. #define SEND_BUFSIZE 8192
  6. void on_timer(htimer_t* timer, void* userdata) {
  7. static int cnt = 0;
  8. printf("on_timer timer_id=%d time=%luus cnt=%d\n", timer->timer_id, timer->loop->cur_time, ++cnt);
  9. }
  10. void on_idle(hidle_t* idle, void* userdata) {
  11. static int cnt = 0;
  12. printf("on_idle idle_id=%d cnt=%d\n", idle->idle_id, ++cnt);
  13. }
  14. void on_read(hevent_t* event, void* userdata) {
  15. printf("on_read fd=%d\n", event->fd);
  16. char recvbuf[RECV_BUFSIZE] = {0};
  17. int nrecv;
  18. recv:
  19. memset(recvbuf, 0, sizeof(recvbuf));
  20. nrecv = recv(event->fd, recvbuf, sizeof(recvbuf), 0);
  21. printf("recv retval=%d\n", nrecv);
  22. if (nrecv < 0) {
  23. if (sockerrno == NIO_EAGAIN) {
  24. //goto recv_done;
  25. return;
  26. }
  27. else {
  28. perror("recv");
  29. goto recv_error;
  30. }
  31. }
  32. if (nrecv == 0) {
  33. goto disconnect;
  34. }
  35. printf("> %s\n", recvbuf);
  36. if (nrecv == sizeof(recvbuf)) {
  37. goto recv;
  38. }
  39. recv_done:
  40. return;
  41. recv_error:
  42. disconnect:
  43. printf("closesocket fd=%d\n", event->fd);
  44. closesocket(event->fd);
  45. hevent_del(event);
  46. }
  47. void on_stdin(hevent_t* event, void* userdata) {
  48. printf("on_stdin fd=%d\n", event->fd);
  49. int connfd = (int)(long)userdata;
  50. char sendbuf[RECV_BUFSIZE] = {0};
  51. int nread, nsend;
  52. read:
  53. memset(sendbuf, 0, sizeof(sendbuf));
  54. nread = read(0, sendbuf, sizeof(sendbuf));
  55. send:
  56. nsend = send(connfd, sendbuf, nread, 0);
  57. printf("send retval=%d\n", nsend);
  58. printf("< %s\n", sendbuf);
  59. }
  60. void on_connect(hevent_t* event, void* userdata) {
  61. printf("on_connect connfd=%d\n", event->fd);
  62. struct sockaddr_in localaddr,peeraddr;
  63. socklen_t addrlen = sizeof(struct sockaddr_in);
  64. int ret = getpeername(event->fd, (struct sockaddr*)&peeraddr, &addrlen);
  65. if (ret < 0) {
  66. printf("connect failed: %s: %d\n", strerror(sockerrno), sockerrno);
  67. closesocket(event->fd);
  68. return;
  69. }
  70. addrlen = sizeof(struct sockaddr_in);
  71. getsockname(event->fd, (struct sockaddr*)&localaddr, &addrlen);
  72. printf("connect connfd=%d [%s:%d] => [%s:%d]\n", event->fd,
  73. inet_ntoa(localaddr.sin_addr), ntohs(localaddr.sin_port),
  74. inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port));
  75. hevent_read(event->loop, event->fd, on_read, NULL);
  76. hevent_read(event->loop, 0, on_stdin, (void*)(long)event->fd);
  77. }
  78. int main(int argc, char** argv) {
  79. if (argc < 3) {
  80. printf("Usage: cmd host port\n");
  81. return -10;
  82. }
  83. const char* host = argv[1];
  84. int port = atoi(argv[2]);
  85. int connfd = Connect(host, port, 1);
  86. printf("connfd=%d\n", connfd);
  87. if (connfd < 0) {
  88. return connfd;
  89. }
  90. hloop_t loop;
  91. hloop_init(&loop);
  92. //hidle_add(&loop, on_idle, NULL);
  93. //htimer_add(&loop, on_timer, NULL, 1000, INFINITE);
  94. hevent_connect(&loop, connfd, on_connect, NULL);
  95. hloop_run(&loop);
  96. return 0;
  97. }