当前位置:   article > 正文

libevent for echo server and client_libevent echo server

libevent echo server

echo server:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <assert.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <event2/event.h>
  8. #include <event2/bufferevent.h>
  9. #define LISTEN_PORT 9999
  10. #define LISTEN_BACKLOG 32
  11. #define MAX_LINE 256
  12. void do_accept(evutil_socket_t listener, short event, void *arg);
  13. void read_cb(struct bufferevent *bev, void *arg);
  14. void error_cb(struct bufferevent *bev, short event, void *arg);
  15. void write_cb(struct bufferevent *bev, void *arg);
  16. int main()
  17. {
  18. //int ret;
  19. evutil_socket_t listener;//用于跨平台表示socket的ID(在Linux下表示的是其文件描述符)
  20. listener = socket(AF_INET, SOCK_STREAM, 0);
  21. assert(listener > 0);
  22. //用于跨平台将socket设置为可重用(实际上是将端口设为可重用
  23. evutil_make_listen_socket_reuseable(listener);
  24. struct sockaddr_in sin;
  25. sin.sin_family = AF_INET;
  26. sin.sin_addr.s_addr = inet_addr("127.0.0.1");
  27. sin.sin_port = htons(LISTEN_PORT);
  28. if (bind(listener, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  29. perror("bind");
  30. return 1;
  31. }
  32. if (listen(listener, LISTEN_BACKLOG) < 0) {
  33. perror("listen");
  34. return 1;
  35. }
  36. printf ("Listening...\n");
  37. /* 用于跨平台将socket设置为非阻塞,使用bufferevent需要 */
  38. evutil_make_socket_nonblocking(listener);
  39. //主要记录事件的相关属性
  40. struct event_base *base = event_base_new();
  41. assert(base != NULL);
  42. /* Register listen event. */
  43. struct event *listen_event;
  44. listen_event = event_new(base, listener, EV_READ|EV_PERSIST, do_accept, (void*)base);
  45. event_add(listen_event, NULL);
  46. /* Start the event loop. */
  47. event_base_dispatch(base);
  48. printf("The End.");
  49. //close(listener);
  50. return 0;
  51. }
  52. void do_accept(evutil_socket_t listener, short event, void *arg)
  53. {
  54. struct event_base *base = (struct event_base *)arg;
  55. evutil_socket_t fd;
  56. struct sockaddr_in sin;
  57. socklen_t slen = sizeof(sin);
  58. fd = accept(listener, (struct sockaddr *)&sin, &slen);
  59. if (fd < 0) {
  60. perror("accept");
  61. return;
  62. }
  63. if (fd > FD_SETSIZE) { //这个if是参考了那个ROT13的例子,貌似是官方的疏漏,从select-based例子里抄过来忘了改
  64. perror("fd > FD_SETSIZE\n");
  65. return;
  66. }
  67. printf("ACCEPT: fd = %u\n", fd);
  68. //关联该sockfd,托管给event_base.
  69. struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
  70. //设置读写对应的回调函数
  71. bufferevent_setcb(bev, read_cb, NULL, error_cb, arg);
  72. //启用读写事件,其实是调用了event_add将相应读写事件加入事件监听队列poll.
  73. //如果相应事件不置为true,bufferevent是不会读写数据的
  74. bufferevent_enable(bev, EV_READ|EV_PERSIST);
  75. // 进入bufferevent_setcb回调函数:
  76. // 在readcb里面从input中读取数据,处理完毕后填充到output中;
  77. // writecb对于服务端程序,只需要readcb就可以了,可以置为NULL;
  78. // errorcb用于处理一些错误信息
  79. }
  80. void read_cb(struct bufferevent *bev, void *arg)
  81. {
  82. char line[MAX_LINE+1];
  83. int n;
  84. evutil_socket_t fd = bufferevent_getfd(bev);
  85. while (n = bufferevent_read(bev, line, MAX_LINE), n > 0)
  86. {
  87. line[n] = '\0';
  88. printf("fd=%u, Server gets the message from client read line: %s\n", fd, line);
  89. //直接将读取的结果,不做任何修改(本文是跳过前两个字符),直接返回给客户端
  90. bufferevent_write(bev, line, n);//方案1
  91. }
  92. }
  93. void write_cb(struct bufferevent *bev, void *arg)
  94. {
  95. printf("HelloWorld\n");//直接空代码即可,因为这里并不会被触发调用
  96. }
  97. void error_cb(struct bufferevent *bev, short event, void *arg)
  98. {
  99. evutil_socket_t fd = bufferevent_getfd(bev);
  100. printf("fd = %u, ", fd);
  101. if (event & BEV_EVENT_TIMEOUT) {
  102. printf("Timed out\n"); //if bufferevent_set_timeouts() called
  103. }
  104. else if (event & BEV_EVENT_EOF) {
  105. printf("connection closed\n");
  106. }
  107. else if (event & BEV_EVENT_ERROR) {
  108. printf("some other error\n");
  109. }
  110. bufferevent_free(bev);
  111. }
client code:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <netinet/in.h>
  7. #include <sys/socket.h>
  8. #include <arpa/inet.h>
  9. #include <sys/types.h>
  10. #include <event2/event.h>
  11. #include <event2/bufferevent.h>
  12. #define SERV_PORT 9999
  13. #define MAX_LINE 1024
  14. void cmd_msg_cb(int fd, short event, void *arg);
  15. void read_cb(struct bufferevent *bev, void *arg);
  16. void error_cb(struct bufferevent *bev, short event, void *arg);
  17. int main(int argc, char *argv[])
  18. {
  19. char* ip_str = "127.0.0.1";
  20. evutil_socket_t sockfd;
  21. if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  22. {
  23. perror("socket\n");
  24. return 1;
  25. }
  26. struct sockaddr_in servaddr;
  27. bzero(&servaddr, sizeof(servaddr));
  28. servaddr.sin_family = AF_INET;
  29. servaddr.sin_port = htons(SERV_PORT);
  30. if(inet_pton(AF_INET, ip_str, &servaddr.sin_addr) < 1)
  31. {
  32. perror("inet_ntop\n");
  33. return 1;
  34. }
  35. if(connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
  36. {
  37. perror("connect\n");
  38. return 1;
  39. }
  40. evutil_make_socket_nonblocking(sockfd);
  41. printf("Connect to server sucessfully!\n");
  42. // build event base
  43. struct event_base *base = event_base_new();
  44. if(base == NULL)
  45. {
  46. perror("event_base\n");
  47. return 1;
  48. }
  49. const char *eventMechanism = event_base_get_method(base);
  50. printf("Event mechanism used is %s\n", eventMechanism);
  51. printf("sockfd = %d\n", sockfd);
  52. struct bufferevent *bev = bufferevent_socket_new(base, sockfd, BEV_OPT_CLOSE_ON_FREE);
  53. struct event *ev_cmd;
  54. ev_cmd = event_new(base, STDIN_FILENO, EV_READ | EV_PERSIST, cmd_msg_cb, (void *)bev);
  55. event_add(ev_cmd, NULL);
  56. bufferevent_setcb(bev, read_cb, NULL, error_cb, (void *)ev_cmd);
  57. bufferevent_enable(bev, EV_READ | EV_PERSIST);
  58. event_base_dispatch(base);
  59. printf("The End.");
  60. return 0;
  61. }
  62. void cmd_msg_cb(int fd, short event, void *arg)
  63. {
  64. char msg[MAX_LINE];
  65. int nread = read(fd, msg, sizeof(msg));
  66. if(nread < 0)
  67. {
  68. perror("stdio read fail\n");
  69. return;
  70. }
  71. struct bufferevent *bev = (struct bufferevent *)arg;
  72. bufferevent_write(bev, msg, nread);
  73. }
  74. void read_cb(struct bufferevent *bev, void *arg)
  75. {
  76. char line[MAX_LINE + 1];
  77. int n;
  78. evutil_socket_t fd = bufferevent_getfd(bev);
  79. while((n = bufferevent_read(bev, line, MAX_LINE)) > 0)
  80. {
  81. line[n] = '\0';
  82. printf("fd = %u, read from server: %s", fd, line);
  83. }
  84. }
  85. void error_cb(struct bufferevent *bev, short event, void *arg)
  86. {
  87. evutil_socket_t fd = bufferevent_getfd(bev);
  88. printf("fd = %u, ", fd);
  89. if(event & BEV_EVENT_TIMEOUT)
  90. printf("Time out.\n"); // if bufferevent_set_timeouts() is called
  91. else if(event & BEV_EVENT_EOF)
  92. printf("Connection closed.\n");
  93. else if(event & BEV_EVENT_ERROR)
  94. printf("Some other error.\n");
  95. bufferevent_free(bev);
  96. struct event *ev = (struct event *)arg;
  97. event_free(ev);
  98. }

buind:


runing:



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/518105
推荐阅读
相关标签
  

闽ICP备14008679号