- 论坛徽章:
- 0
|
麻烦大家看一下这段代码,客户端在断开时,服务器端有时能断开,有时不能!基本上是5~6个才能断开一个!
- /*---------------------------------------------------------
- * TestEpoll.cpp
- ---------------------------------------------------------*/
- #include <iostream>;
- #include <string>;
- #include <sys/epoll.h>;
- #include <sys/time.h>;
- #include <sys/types.h>;
- #include <sys/socket.h>;
- #include <sys/wait.h>;
- #include <netdb.h>;
- #include <ctype.h>;
- #include <arpa/inet.h>;
- #include <fcntl.h>;
- #include <signal.h>;
- #include <errno.h>;
- using namespace std;
- #define MAX_CLIENT 10000
- #define PORT 8008
- int ListenFd = -1;
- void nonblock(int sockfd)
- {
- int opts;
- opts = fcntl(sockfd, F_GETFL);
- if(opts < 0)
- {
- cout<<" fcntl(F_GETFL)"<<endl;
- exit(1);
- }
- opts = (opts | O_NONBLOCK);
- if(fcntl(sockfd, F_SETFL, opts) < 0)
- {
- cout<<"fcntl(F_SETFL)"<<endl;
- exit(1);
- }
- }
- int main()
- {
- struct sockaddr_in srv;
- int clifd;
- if((ListenFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
- {
- cout<<" Error Create Socket !"<<endl;
- exit(1);
- }
- int flags, ling;
- setsockopt(ListenFd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
- setsockopt(ListenFd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags));
- setsockopt(ListenFd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
- bzero(&srv, sizeof(srv));
- srv.sin_family = AF_INET;
- srv.sin_addr.s_addr = INADDR_ANY;
- srv.sin_port = htons(PORT);
- if(bind(ListenFd, (struct sockaddr*)&srv, sizeof(srv)) < 0)
- {
- cout<<" Error Bind Port !"<<endl;
- exit(1);
- }
- listen(ListenFd, 1024);
- int epfd = epoll_create(MAX_CLIENT);
- if(!epfd)
- {
- cout<<" Error Create Epoll !"<<endl;
- exit(1);
- }
- struct epoll_event ev;
- ev.events = EPOLLIN | EPOLLET;
- ev.data.fd = ListenFd;
- if(epoll_ctl(epfd, EPOLL_CTL_ADD, ListenFd, &ev) < 0)
- {
- cout<<" Error Add ListenFd to Epoll !"<<endl;
- exit(1);
- }
- char buffer[1024];
- for(;;)
- {
- struct epoll_event *events = new struct epoll_event;
- int res = epoll_wait(epfd, events, MAX_CLIENT, -1);
- for(int i=0; i<res; i++)
- {
- if(events[i].data.fd == ListenFd)
- {
- clifd = accept(ListenFd, NULL, NULL);
- cout<<"LINK: "<<clifd<<endl;
- if(clifd >;= 0)
- {
- nonblock(clifd);
- ev.events = EPOLLIN | EPOLLET;
- ev.data.fd = clifd;
- if(epoll_ctl(epfd, EPOLL_CTL_ADD, clifd, &ev) < 0)
- {
- cout<<" Error EPOLL_CTL_ADD !"<<endl;
- exit(1);
- }
- }
- }
- else
- {
- bzero(buffer, strlen(buffer));
- int n = recv(events[i].data.fd, buffer, 1024, 0);
- if(n == 0)
- {
- cout<<events[i].data.fd<<" Closeed Connection !"<<endl;
- epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
- close(events[i].data.fd);
- }
- else if(n < 0)
- {
- epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
- cout<<events[i].data.fd<<" Error !"<<endl;
- close(events[i].data.fd);
- }
- else
- {
- cout<<"SOCK: "<<events[i].data.fd<<"RECV: "<<buffer<<endl;
- bzero(&buffer, strlen(buffer));
- if(send(events[i].data.fd, buffer, strlen(buffer), 0) < 0)
- cout<<"Send Error"<<endl;
- }
- }
- }
- delete events;
- }
- }
复制代码 |
|