10.1 Hello_World服务器

  1. #include <string.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <signal.h>
  5. #ifndef WIN32
  6. #include <netinet/in.h>
  7. # ifdef _XOPEN_SOURCE_EXTENDED
  8. # include <arpa/inet.h>
  9. # endif
  10. #include <sys/socket.h>
  11. #endif
  12. #include <event2/bufferevent.h>
  13. #include <event2/buffer.h>
  14. #include <event2/listener.h>
  15. #include <event2/util.h>
  16. #include <event2/event.h>
  17. static const char MESSAGE[] = "Hello, World!\n";
  18. static const int PORT = 9995;
  19. static void listener_cb(struct evconnlistener *, evutil_socket_t,
  20. struct sockaddr *, int socklen, void *);
  21. static void conn_writecb(struct bufferevent *, void *);
  22. static void conn_eventcb(struct bufferevent *, short, void *);
  23. static void signal_cb(evutil_socket_t, short, void *);
  24. int
  25. main(int argc, char **argv)
  26. {
  27. struct event_base *base;
  28. struct evconnlistener *listener;
  29. struct event *signal_event;
  30. struct sockaddr_in sin;
  31. #ifdef WIN32
  32. WSADATA wsa_data;
  33. WSAStartup(0x0201, &wsa_data);
  34. #endif
  35. base = event_base_new();
  36. if (!base) {
  37. fprintf(stderr, "Could not initialize libevent!\n");
  38. return 1;
  39. }
  40. memset(&sin, 0, sizeof(sin));
  41. sin.sin_family = AF_INET;
  42. sin.sin_port = htons(PORT);
  43. listener = evconnlistener_new_bind(base, listener_cb, (void *)base,
  44. LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1,
  45. (struct sockaddr*)&sin,
  46. sizeof(sin));
  47. if (!listener) {
  48. fprintf(stderr, "Could not create a listener!\n");
  49. return 1;
  50. }
  51. signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
  52. if (!signal_event || event_add(signal_event, NULL)<0) {
  53. fprintf(stderr, "Could not create/add a signal event!\n");
  54. return 1;
  55. }
  56. event_base_dispatch(base);
  57. evconnlistener_free(listener);
  58. event_free(signal_event);
  59. event_base_free(base);
  60. printf("done\n");
  61. return 0;
  62. }
  63. static void
  64. listener_cb(struct evconnlistener *listener, evutil_socket_t fd,
  65. struct sockaddr *sa, int socklen, void *user_data)
  66. {
  67. struct event_base *base = user_data;
  68. struct bufferevent *bev;
  69. bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
  70. if (!bev) {
  71. fprintf(stderr, "Error constructing bufferevent!");
  72. event_base_loopbreak(base);
  73. return;
  74. }
  75. bufferevent_setcb(bev, NULL, conn_writecb, conn_eventcb, NULL);
  76. bufferevent_enable(bev, EV_WRITE);
  77. bufferevent_disable(bev, EV_READ);
  78. bufferevent_write(bev, MESSAGE, strlen(MESSAGE));
  79. }
  80. static void
  81. conn_writecb(struct bufferevent *bev, void *user_data)
  82. {
  83. struct evbuffer *output = bufferevent_get_output(bev);
  84. if (evbuffer_get_length(output) == 0) {
  85. printf("flushed answer\n");
  86. bufferevent_free(bev);
  87. }
  88. }
  89. static void
  90. conn_eventcb(struct bufferevent *bev, short events, void *user_data)
  91. {
  92. if (events & BEV_EVENT_EOF) {
  93. printf("Connection closed.\n");
  94. } else if (events & BEV_EVENT_ERROR) {
  95. printf("Got an error on the connection: %s\n",
  96. strerror(errno));/*XXX win32*/
  97. }
  98. /* None of the other events can happen here, since we haven't enabled
  99. * timeouts */
  100. bufferevent_free(bev);
  101. }
  102. static void
  103. signal_cb(evutil_socket_t sig, short events, void *user_data)
  104. {
  105. struct event_base *base = user_data;
  106. struct timeval delay = { 2, 0 };
  107. printf("Caught an interrupt signal; exiting cleanly in two seconds.\n");
  108. event_base_loopexit(base, &delay);
  109. }