返回信息流LT模式
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
bool setnonblocking(int& fd)
{
int opt;
opt = fcntl(fd, F_GETFL);
if(opt < 0)
{
perror("get fd error");
return false;
}
opt |= O_NONBLOCK;
if(fcntl(fd, F_SETFL, opt) < 0)
{
perror("set fd error");
return false;
}
return true;
}
int do_use_fd(int fd)
{
int n;
char buff[1024] = {0};
n = read(fd, buff, 1);
if(n == -1)
{
if(errno == EAGAIN)
return 0;
}
else if(n == 0)
{
printf("client close fd\n");
close(fd);
}
else
{
printf("read %s\n", buff);
}
return 0;
}
int main()
{
struct sockaddr_in addr;
int listenfd;
int epollfd;
struct epoll_event pev[1024], ev;
bzero(pev, sizeof(pev));
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(12345);
if(inet_pton(AF_INET, "10.1.130.5", &addr.sin_addr) < 0)
{
perror("inet_pton error");
return -1;
}
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(listenfd < 0)
{
perror("create socket error");
return -1;
}
/*
*/
if(!setnonblocking(listenfd))
{
perror("set nonblocking error");
return -1;
}
if(bind(listenfd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
{
perror("bind error");
return -1;
}
if(listen(listenfd, 24) == -1)
{
perror("listen error");
return -1;
}
epollfd = epoll_create(1024);
if(epollfd == -1)
{
perror("epoll create error");
return -1;
}
ev.events = EPOLLIN;
ev.data.fd = listenfd;
if(epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &ev) < 0)
{
perror("epoll ctl error");
return -1;
}
struct sockaddr_in remoteAddr;
socklen_t remoteAddrLen = sizeof(remoteAddr);
int nfds;
int i;
while(1)
{
nfds=epoll_wait(epollfd, pev, 1024, -1);
for(i=0; i<nfds; i++)
{
if(pev[i].data.fd == listenfd)
{
int connd = accept(listenfd, (struct sockaddr*)&remoteAddr, &remoteAddrLen);
if(connd == -1)
{
if(errno == EWOULDBLOCK)
{
printf("none connections\n");
continue;
}
perror("accept error");
return -1;
}
printf("accept\n");
struct epoll_event ev;
ev.data.fd = connd;
ev.events |= EPOLLIN;
epoll_ctl(epollfd, EPOLL_CTL_ADD, connd, &ev);
}
else if(pev[i].events & (EPOLLHUP|EPOLLERR))
{
close(pev[i].data.fd);
}
else if(pev[i].events & EPOLLIN)
{
do_use_fd(pev[i].data.fd);
//close(pev[i].data.fd);
}
else
{
printf("epoll waiting ...\n");
}
}
printf("epoll waiting ...\n");
}
return 0;
}
##########################
ET模式
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
bool setnonblocking(int& fd)
{
int opt;
opt = fcntl(fd, F_GETFL);
if(opt < 0)
{
perror("get fd error");
return false;
}
opt |= O_NONBLOCK;
if(fcntl(fd, F_SETFL, opt) < 0)
{
perror("set fd error");
return false;
}
return true;
}
int do_use_fd(int fd)
{
int n;
char buff[1024] = {0};
while(1)
{
n = read(fd, buff, 1);
if(n == -1)
{
if(errno == EAGAIN)
break;
}
else if(n == 0)
{
break;
}
else
{
printf("read %s\n", buff);
memset(buff, 0, 1024);
}
}
return 0;
}
int main()
{
struct sockaddr_in addr;
int listenfd;
int epollfd;
struct epoll_event pev[1024], ev;
bzero(pev, sizeof(pev));
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(12345);
if(inet_pton(AF_INET, "10.1.130.5", &addr.sin_addr) < 0)
{
perror("inet_pton error");
return -1;
}
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(listenfd < 0)
{
perror("create socket error");
return -1;
}
/*
*/
if(!setnonblocking(listenfd))
{
perror("set nonblocking error");
return -1;
}
if(bind(listenfd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
{
perror("bind error");
return -1;
}
if(listen(listenfd, 24) == -1)
{
perror("listen error");
return -1;
}
epollfd = epoll_create(1024);
if(epollfd == -1)
{
perror("epoll create error");
return -1;
}
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = listenfd;
if(epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &ev) < 0)
{
perror("epoll ctl error");
return -1;
}
struct sockaddr_in remoteAddr;
socklen_t remoteAddrLen = sizeof(remoteAddr);
int nfds;
int i;
while(1)
{
nfds=epoll_wait(epollfd, pev, 1024, -1);
for(i=0; i<nfds; i++)
{
if(pev[i].data.fd == listenfd)
{
while(1)
{
int connd = accept(listenfd, (struct sockaddr*)&remoteAddr, &remoteAddrLen);
if(connd == -1)
{
if(errno == EWOULDBLOCK || errno == EAGAIN)
{
printf("none connections\n");
break;
}
perror("accept error");
return -1;
}
printf("accept\n");
struct epoll_event ev;
ev.data.fd = connd;
ev.events = EPOLLIN | EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_ADD, connd, &ev);
}
}
else if(pev[i].events & (EPOLLHUP|EPOLLERR))
{
close(pev[i].data.fd);
}
else if(pev[i].events & EPOLLIN)
{
do_use_fd(pev[i].data.fd);
close(pev[i].data.fd);
}
else
{
printf("epoll waiting ...\n");
}
}
printf("epoll waiting ...\n");
}
return 0;
}
求人不如求自己,动手做一下就明白了。
这是一条镜像帖。来源:北邮人论坛 / soft-design / #27248同步于 2008/6/23
SoftDesign机器人发帖
epoll的两种触发方式
redfox
2008/6/23镜像同步0 回复
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。