c5395348
c5395348
采纳率28.6%
2015-09-30 08:30

Linux 网络编程 epoll中的EPOLLIN EPOLLOUT如何触发

代码很长只截取关键部分
//服务器端
#include
#include
#include
#include
#include
#include
#include
#include
#include

#include"locker.h"
#include"threadpool.h"
#include"http_conn.h"
#include"http_conn.cpp"
extern void addfd(int epollfd,int fd,bool one_shot);
extern void removefd(int epollfd,int fd);
#define MAX_FD 65536
#define MAX_EVENT_NUMBER 10000
void addsig(int sig,void(handler)(int),bool restart=true)
{
struct sigaction sa;
memset(&sa,'\0',sizeof(sa));
sa.sa_handler=handler;
if(restart)
{
sa.sa_flags|=SA_RESTART;
}
sigfillset(&sa.sa_mask);
assert(sigaction(sig,&sa,NULL)!=-1);
}

void show_error(int connfd,const char* info)
{
printf("%s",info);
send(connfd,info,strlen(info),0);
close(connfd);
}
int main(int argc,char* argv[])
{
if(argc {
printf("usage: %s ip_address port_number\n",basename(argv[0]));
return 1;
}
const char* ip=argv[1];
int port=atoi(argv[2]);
addsig(SIGPIPE,SIG_IGN);
threadpool* pool=NULL;

pool=new threadpool<http_conn>(3);

http_conn* users=new http_conn[MAX_FD];
assert(users);
int user_count=0;
int listenfd=socket(PF_INET,SOCK_STREAM,0);
assert(listenfd>=0);
struct linger tmp={1,0};
setsockopt(listenfd,SOL_SOCKET,SO_LINGER,&tmp,sizeof(tmp));

int ret=0;
struct sockaddr_in address;
bzero(&address,sizeof(address));
address.sin_family=AF_INET;
inet_pton(AF_INET,ip,&address.sin_addr);
address.sin_port=htons(port);

ret=bind(listenfd,(struct sockaddr*)&address,sizeof(address));
assert(ret>=0);

ret=listen(listenfd,5);
assert(ret>=0);

epoll_event events[MAX_EVENT_NUMBER];
int epollfd=epoll_create(5);
assert(epollfd!=-1);
addfd(epollfd,listenfd,false);
http_conn::m_epollfd=epollfd;
while(1)
{
    int number=epoll_wait(epollfd,events,MAX_EVENT_NUMBER,-1);
    printf("number is %d\n",number);
    if((number<0)&&(errno!=EINTR))
    {
        printf("epoll failure\n");
        break;
    }
    for(int i=0;i<number;i++)
    {
        int sockfd=events[i].data.fd;
        if(sockfd==listenfd)
        {
            struct sockaddr_in client_address;
            socklen_t client_addrlength=sizeof(client_address);
            int connfd=accept(listenfd,(struct sockaddr*)&client_address,&client_addrlength);
            if(connfd<0)
            {
                printf("errno is: %d\n",errno);
                continue;
            }
            if(http_conn::m_user_count>=MAX_FD)
            {
                show_error(connfd,"Internal sever busy");
                continue;
            }
            printf("running the init(connfd,client_address)"\n);
            users[connfd].init(connfd,client_address);

        }
        else if(events[i].events&(EPOLLRDHUP|EPOLLHUP|EPOLLERR))
        {
            users[sockfd].close_conn();
        }
        else if(events[i].events&EPOLLIN)
        {
            if(users[sockfd].read())
            {
                pool->append(users+sockfd);
            }
            else
            {
                users[sockfd].close_conn();
            }
        }
        else if(events[i].events&EPOLLOUT)
        {
            if(!users[sockfd].write())
            {
                users[sockfd].close_conn();
            }
        }
    }
}
close(epollfd);
close(listenfd);
delete [] users;
delete pool;
return 0;

}

以上是服务器端的主程序 思路是epoll_wait接收到连接就为连接创建一个users存储然后等待后续的操作 但后面EPOLLIN 和EPOLLOUT永远都没法触发 不清楚该怎么触发 另一端写了服务器压力测试程序 和以上代码类似 就是循环创建socket对象然后connect()服务器 但我本意想两端互相发送数据 可connect()后服务器收到创建一个user 继续循环等待 但压力测试程序也在创建完对象后陷入循环等待服务器端的操作 请问该如何触发EPOLLIN和EPOLLOUT信号

以下是压力测试程序关键代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static const char* request = "GET http://localhost/index.html HTTP/1.1\r\nConnection: keep-alive\r\n\r\nxxxxxxxxxx";
int setnonblocking(int fd)
{
int old_option=fcntl(fd,F_GETFL);
int new_option=old_option|O_NONBLOCK;
fcntl(fd,F_SETFL,new_option);
return old_option;
}
void addfd(int epollfd,int fd)
{
epoll_event event;
event.data.fd=fd;
event.events=EPOLLIN|EPOLLET|EPOLLRDHUP;
epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&event);
setnonblocking(fd);
}

bool write_nbytes(int sockfd,const char* buffer,int len)
{
int byters_write=0;
printf("write out %d bytes to socket %d\n",len,sockfd);
while(1)
{
bytes_write=send(sockfd,buffer,len,0);
if(bytes_write==-1)
{
return false;
}
else if(bytes_write==0)
{
return false;
}
len-=bytes_write;
buffer=buffer+bytes_write;
if(len<=0)
{
return true;
}
}
}

bool read_once(int sockfd,char* buffer,int len)
{
int bytes_read=0;
memset(buffer,'\0',len);
bytes_read=recv(sockfd,buffer,len,0);
if(bytes_read==-1)
{
return false;
}
else if(bytes_read==0)
{
return false;
}
printf("read in %d bytes from socket %d with content: %s\n",bytes_read,sockfd,buffer);
return true;
}
void start_conn(int epoll_fd,int num,const char* ip,int port)
{
int ret=0;
struct sockaddr_in address;
bzero(&address,sizeof(address));
address.sin_family=AF_INET;
inet_pton(AF_INET,ip,&address.sin_addr);
address.sin_port=htons(port);

for(int i=0;i<num;++i)
{
    sllep(1);
    int sockfd=socket(PF_INET,SOCK_STREAM,0);
    printf("create 1 sock\n");
    if(sockfd<0)
    {
        continue;
    }
    if(connect(sockfd,(struct sockaddr*)&address,sizeof(address))==0)
    {
        printf("build connection %d\n",i);
        addfd(epoll_fd,sockfd);
    }
}

}
void close_conn(int epoll_fd,int sockfd)
{
epoll_ctl(epoll_fd,EPOLL_CTL_DEL,sockfd,0);
close(sockfd);
}

int main(int argc,char* argv[])
{
assert(argc==4);
int epoll_fd=epoll_create(100);
start_conn(epoll_fd,atoi(argv[3]),argv[1],atoi(argv[2]));
epoll_event events[10000];
char buffer[2048];

while(1)
{
    int fds=epoll_wait(epoll_fd,events,10000,2000);
    for(int i=0;i<fds;i++)
    {
        int sockfd=events[i].data.fd;
        if(event[i].events&EPOLLIN)
        {
            if(!read_once(sockfd,buffer,2048));
            {
                close_conn(epoll_fd,sockfd);
            }
            struct epoll_event event;
            event.events=EPOLLOUT|EPOLLET|EPOLLERR;
            event.data.fd=sockfd;
            epoll_ctl(epoll_fd,EPOLL_CTL_MOD,sockfd,&event);
        }
        else if(events[i].events&EPOLLOUT)
        {
            if(!write_nbytes(sockfd,request,strlen(request)))
            {
                close_conn(epoll_fd,sockfd);
            }
            struct epoll_event event;
            event.events=EPOLLIN|EPOLLET|EPOLLERR;
            event.data.fd=sockfd;
            epoll_ctl(epoll_fd,EPOLL_CTL_MOD,sockfd,&event);
        }
        else if(events[i].events&EPOLLERR)
        {
            close_conn(epoll_fd,sockfd);
        }
    }
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • geqiandebei 千山~ 6年前

    你的得先注册事件监听啊。把要监控的套接字和要监听的事件注册

    点赞 评论 复制链接分享

相关推荐