- 论坛徽章:
- 0
|
多线程和epoll 问题, epoll_wait 不返回
设计的思想是这样的。
起一个子线程,这个线程称会不断建立与服务器的连接。
主线程则会 不断在epoll_wait 等待返回然后处理。 我这里epoll_wait 不返回。
通过网络监控 发现 子线程的的连接应该建立起来了
有三步握手了。 但我的程序一直不返回
还有2点 read write 的 阻塞和非阻塞有什么区别吗?
connect 的阻塞和非阻塞有什么区别?
- #include <pthread.h>
- #include <stdio.h>
- #include <sys/epoll.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <fcntl.h>
- #define maxevents 65535
- #define max_thread 6000
- void *work(void *arg);
- void setnonblocking(int sock);
- void do_use_fd(struct epoll_event *events);
- int lines ;
- int epollfd = 0;
- int main(){
- pthread_t pid_t;
- struct epoll_event ev, *events;
- int nfds = 0;
- int n = 0;
- int status;
-
- status = pthread_create(&pid_t, NULL, work, NULL); // 起一个线称建立连接
-
- if (status == 0) {
- printf(" %d created \n", n);
- } else {
- printf(" %d create fail\n", n);
- exit(0);
- }
-
- epollfd = epoll_create(maxevents); // 建立epoll
-
- if (-1 == epollfd){
- printf("error cannot epoll_create\n");
- return 0;
- }
-
- for (;;){
- nfds = epoll_wait(epollfd, events, maxevents, -1); // 开始抓回复
- for(n = 0; n < nfds; ++n) {
- printf("do do_use \n");
- do_use_fd(events+n);
- }
- }
- return 0;
- }
- void *work(void *arg){ // 这个就是工作线称主要发起连接
- int i = 0 ;
- struct sockaddr_in clientadd;
- int client;
- struct epoll_event ev;
- pthread_detach(pthread_self()); // this
- int len = 0;
- for (i = 0 ; i < 5 ;i ++){
-
- sleep(10);
- client = socket(AF_INET , SOCK_STREAM, 0);
- if (client < 0){
- continue;
- }
- printf("begin memset \n");
- memset(&clientadd,0 , sizeof(clientadd));
- clientadd.sin_family = AF_INET;
- clientadd.sin_addr.s_addr = inet_addr("192.168.1.150");
- clientadd.sin_port = htons(80);
- setnonblocking(client);
-
- ev.events = EPOLLIN|EPOLLOUT |EPOLLERR;
- ev.data.fd = client;
- if (epoll_ctl(epollfd, EPOLL_CTL_ADD, client, &ev) < 0) {
- fprintf(stderr, "epoll set insertion error: fd=%d",client);
- return NULL;
- }
- printf("begin connect \n");
- connect(client, (struct sockaddr*)&clientadd, sizeof(clientadd));
- printf("begin connect \n");
- sleep(5);
-
- }
- }
- void setnonblocking(int sock){
- int opts;
- opts=fcntl(sock,F_GETFL);
- if(opts<0)
- {
- perror("fcntl(sock,GETFL)");
- exit(1);
- }
- opts = opts|O_NONBLOCK;
- if(fcntl(sock,F_SETFL,opts)<0)
- {
- perror("fcntl(sock,SETFL,opts)");
- exit(1);
- }
- }
- void do_use_fd(struct epoll_event *events){
- struct epoll_event ev;
- int n ;
- int client = events->data.fd;
- char buff[] ="get / http/1.1\r\n\r\n";
- char inbuff[1024] ={0};
- if (events->events |EPOLLIN){ // 可以读了
- while (read(client, inbuff, 1024)){
- printf("%d get 1024 \n", events->data.fd);
- }
- /*
- ev.events = EPOLLOUT |EPOLLERR;
- ev.data.fd = client;
- if (epoll_ctl(epollfd, EPOLL_CTL_ADD, client, &ev) < 0) {
- fprintf(stderr, "epoll set insertion error: fd=%d",client);
- return -1;
- */
-
- } else if (events->events |EPOLLOUT) {
- write(events->data.fd, buff, strlen(buff));
- return ;
- }
-
- close(events->data.fd); //读完了 就短掉连接
-
- }
复制代码 |
|