1. #include <iostream>
    2. #include <sys/socket.h>
    3. #include <sys/epoll.h>
    4. #include <netinet/in.h>
    5. #include <arpa/inet.h>
    6. #include <fcntl.h>
    7. #include <unistd.h>
    8. #include <stdio.h>
    9. #include <pthread.h>
    10. #include <errno.h>
    11. #define MAXLINE 10
    12. #define OPEN_MAX 100
    13. #define LISTENQ 20
    14. #define SERV_PORT 8006
    15. #define INFTIM 1000
    16. //线程池任务队列结构体
    17. struct task
    18. {
    19. int fd; //需要读写的文件描述符
    20. struct task *next; //下一个任务
    21. };
    22. //用于读写两个的两个方面传递参数
    23. struct user_data
    24. {
    25. int fd;
    26. unsigned int n_size;
    27. char line[MAXLINE];
    28. };
    29. //线程的任务函数
    30. void *readtask(void *args);
    31. void *writetask(void *args);
    32. //声明epoll_event结构体的变量,ev用于注册事件,数组用于回传要处理的事件
    33. struct epoll_event ev, events[20];
    34. int epfd;
    35. pthread_mutex_t mutex;
    36. pthread_cond_t cond1;
    37. struct task *readhead = NULL, *readtail = NULL, *writehead = NULL;
    38. void setnonblocking(int sock)
    39. {
    40. int opts;
    41. opts = fcntl(sock, F_GETFL);
    42. if (opts < 0)
    43. {
    44. perror("fcntl(sock,GETFL)");
    45. exit(1);
    46. }
    47. opts = opts | O_NONBLOCK;
    48. if (fcntl(sock, F_SETFL, opts) < 0)
    49. {
    50. perror("fcntl(sock,SETFL,opts)");
    51. exit(1);
    52. }
    53. }
    54. int main()
    55. {
    56. int i, maxi, listenfd, connfd, sockfd, nfds;
    57. pthread_t tid1, tid2;
    58. struct task *new_task = NULL;
    59. struct user_data *rdata = NULL;
    60. socklen_t clilen;
    61. pthread_mutex_init(&mutex, NULL);
    62. pthread_cond_init(&cond1, NULL);
    63. //初始化用于读线程池的线程
    64. pthread_create(&tid1, NULL, readtask, NULL);
    65. pthread_create(&tid2, NULL, readtask, NULL);
    66. //生成用于处理accept的epoll专用的文件描述符
    67. epfd = epoll_create(256);
    68. struct sockaddr_in clientaddr;
    69. struct sockaddr_in serveraddr;
    70. listenfd = socket(AF_INET, SOCK_STREAM, 0);
    71. //把socket设置为非阻塞方式
    72. setnonblocking(listenfd);
    73. //设置与要处理的事件相关的文件描述符
    74. ev.data.fd = listenfd;
    75. //设置要处理的事件类型
    76. ev.events = EPOLLIN | EPOLLET;
    77. //注册epoll事件
    78. epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &ev);
    79. bzero(&serveraddr, sizeof(serveraddr));
    80. serveraddr.sin_family = AF_INET;
    81. serveraddr.sin_port = htons(SERV_PORT);
    82. serveraddr.sin_addr.s_addr = INADDR_ANY;
    83. bind(listenfd, (sockaddr *)&serveraddr, sizeof(serveraddr));
    84. listen(listenfd, LISTENQ);
    85. maxi = 0;
    86. for (;;)
    87. {
    88. //等待epoll事件的发生
    89. nfds = epoll_wait(epfd, events, 20, 500);
    90. //处理所发生的所有事件
    91. for (i = 0; i < nfds; ++i)
    92. {
    93. if (events[i].data.fd == listenfd)
    94. {
    95. connfd = accept(listenfd, (sockaddr *)&clientaddr, &clilen);
    96. if (connfd < 0)
    97. {
    98. perror("connfd<0");
    99. exit(1);
    100. }
    101. setnonblocking(connfd);
    102. char *str = inet_ntoa(clientaddr.sin_addr);
    103. //std::cout<<"connec_ from >>"<<str<<std::endl;
    104. //设置用于读操作的文件描述符
    105. ev.data.fd = connfd;
    106. //设置用于注测的读操作事件
    107. ev.events = EPOLLIN | EPOLLET;
    108. //注册ev
    109. epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &ev);
    110. }
    111. else if (events[i].events & EPOLLIN)
    112. {
    113. //printf("reading!/n");
    114. if ((sockfd = events[i].data.fd) < 0)
    115. {
    116. continue;
    117. }
    118. new_task = new task();
    119. new_task->fd = sockfd;
    120. new_task->next = NULL;
    121. //添加新的读任务
    122. pthread_mutex_lock(&mutex);
    123. if (readhead == NULL)
    124. {
    125. readhead = new_task;
    126. readtail = new_task;
    127. }
    128. else
    129. {
    130. readtail->next = new_task;
    131. readtail = new_task;
    132. }
    133. //唤醒所有等待cond1条件的线程
    134. pthread_cond_broadcast(&cond1);
    135. pthread_mutex_unlock(&mutex);
    136. }
    137. else if (events[i].events & EPOLLOUT)
    138. {
    139. /*
    140. rdata=(struct user_data *)events[i].data.ptr;
    141. sockfd = rdata->fd;
    142. write(sockfd, rdata->line, rdata->n_size);
    143. delete rdata;
    144. //设置用于读操作的文件描述符
    145. ev.data.fd=sockfd;
    146. //设置用于注测的读操作事件
    147. ev.events=EPOLLIN|EPOLLET;
    148. //修改sockfd上要处理的事件为EPOLIN
    149. epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
    150. */
    151. }
    152. }
    153. }
    154. }
    155. static int count111 = 0;
    156. static time_t oldtime = 0, nowtime = 0;
    157. void *readtask(void *args)
    158. {
    159. int fd = -1;
    160. unsigned int n;
    161. //用于把读出来的数据传递出去
    162. struct user_data *data = NULL;
    163. while (1)
    164. {
    165. pthread_mutex_lock(&mutex);
    166. //等待到任务队列不为空
    167. while (readhead == NULL)
    168. pthread_cond_wait(&cond1, &mutex);
    169. fd = readhead->fd;
    170. //从任务队列取出一个读任务
    171. struct task *tmp = readhead;
    172. readhead = readhead->next;
    173. delete tmp;
    174. pthread_mutex_unlock(&mutex);
    175. data = new user_data();
    176. data->fd = fd;
    177. char recvBuf[1024] = {0};
    178. int ret = 999;
    179. int rs = 1;
    180. while (rs)
    181. {
    182. ret = recv(fd, recvBuf, 1024, 0); // 接受客户端消息
    183. if (ret < 0)
    184. {
    185. //由于是非阻塞的模式,所以当errno为EAGAIN时,表示当前缓冲区已无数据可//读在这里就当作是该次事件已处理过。
    186. if (errno == EAGAIN)
    187. {
    188. printf("EAGAIN\n");
    189. break;
    190. }
    191. else
    192. {
    193. printf("recv error!\n");
    194. close(fd);
    195. break;
    196. }
    197. }
    198. else if (ret == 0)
    199. {
    200. // 这里表示对端的socket已正常关闭.
    201. rs = 0;
    202. }
    203. if (ret == sizeof(recvBuf))
    204. rs = 1; // 需要再次读取
    205. else
    206. rs = 0;
    207. }
    208. if (ret > 0)
    209. {
    210. //-------------------------------------------------------------------------------
    211. data->n_size = n;
    212. count111++;
    213. struct tm *today;
    214. time_t ltime;
    215. time(&nowtime);
    216. if (nowtime != oldtime)
    217. {
    218. printf("%d\n", count111);
    219. oldtime = nowtime;
    220. count111 = 0;
    221. }
    222. char buf[1000] = {0};
    223. sprintf(buf, "HTTP/1.0 200 OK\r\nContent-type: text/plain\r\n\r\n%s", "Hello world!\n");
    224. send(fd, buf, strlen(buf), 0);
    225. close(fd);
    226. }
    227. }
    228. }