linux常用的io多路复用方法,linux socket的IO多路复用简单例子(四)

使用epoll函数编写socket。

原理:使用一个文件描述符来管理多个描述符。epoll只拷贝一次描述符到内核态,当监控的事件发生时,通过函数回调将fd加入到一个就绪表中。接着,检测某个fd是否在就绪表中,存在的话进行对应的读写操作。epoll引入了一个事件结构体。每一个文件描述符对应一个事件结构体。结构体包含文件描述符,需要监控的事件。

server端:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

int main()

{

int server_fd = socket(AF_INET, SOCK_STREAM, 0);

if (server_fd == -1)

{

perror("create server fd error");

return 0;

}

struct sockaddr_in serverAttr;

serverAttr.sin_family = AF_INET;

serverAttr.sin_port = htons(5875);

serverAttr.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(server_fd, (struct sockaddr*)&serverAttr, sizeof(serverAttr)) == -1)

{

perror("bind error");

return 0;

}

if (listen(server_fd, 10))

{

perror("listen error");

return 0;

}

/*

Creates an epoll instance. Returns an fd for the new instance.

The "size" parameter is a hint specifying the number of file

descriptors to be associated with the new instance. The fd

returned by epoll_create() should be closed with close().

int epoll_create (int __size) __THROW;

*/

//创建一个总的文件描述符

int epollFd = epoll_create(1024);

//创建服务器fd所对应的事件结构体

struct epoll_event event;

event.events = EPOLLIN;

event.data.fd = server_fd;

/*

Manipulate an epoll instance "epfd". Returns 0 in case of success,

-1 in case of error ( the "errno" variable will contain the

specific error code ) The "op" parameter is one of the EPOLL_CTL_*

constants defined above. The "fd" parameter is the target of the

operation. The "event" parameter describes which events the caller

is interested in and any associated user data.

int epoll_ctl (int __epfd, int __op, int __fd,

struct epoll_event *__event) __THROW;

*/

//将服务器fd和事件结构体增加到总的fd中

epoll_ctl(epollFd, EPOLL_CTL_ADD, server_fd, &event);

//用来保存系统监控发生后,已经改变的fd事件

struct epoll_event evs[1024];

while (1)

{

/* Wait for events on an epoll instance "epfd". Returns the number of

triggered events returned in "events" buffer. Or -1 in case of

error with the "errno" variable set to the specific error code. The

"events" parameter is a buffer that will contain triggered

events. The "maxevents" is the maximum number of events to be

returned ( usually size of "events" ). The "timeout" parameter

specifies the maximum wait time in milliseconds (-1 == infinite).

int epoll_wait (int __epfd, struct epoll_event *__events,

int __maxevents, int __timeout);

*/

//ret表示发生改变的事件数量

int ret = epoll_wait(epollFd, evs, 100, -1);

for (int i = 0; i < ret; i++)

{

int fd = evs[i].data.fd;

//表示接受到新的请求

if ((fd == server_fd) && (evs[i].events & EPOLLIN))

{

struct sockaddr_in clAttr;

socklen_t len = sizeof(clAttr);

int clFd = accept(server_fd, (struct sockaddr*)&clAttr, &len);

if (clFd == -1)

{

perror("accept error");

continue;

}

cout << "accept client :" << inet_ntoa(clAttr.sin_addr) << ":" << clAttr.sin_port << endl;

//将新的客户端fd和对应的事件添加到总的fd中

struct epoll_event e;

e.events = EPOLLIN;

e.data.fd = clFd;

epoll_ctl(epollFd, EPOLL_CTL_ADD, clFd, &e);

continue;

}

//表示这个fd接受到客户端发送的消息

if (evs[i].events & EPOLLIN)

{

char buf[1024] = {0};

int nread = read(evs[i].data.fd, buf, 1024);

if (nread == -1)

{

perror("read error");

close(fd);

epoll_ctl(epollFd, EPOLL_CTL_DEL, evs[i].data.fd, &evs[i]);

continue;

}

if (nread == 0)

{

perror("server close");

close(fd);

epoll_ctl(epollFd, EPOLL_CTL_DEL, evs[i].data.fd, &evs[i]);

continue;

}

cout << "read msg is " << buf << endl;

evs[i].events = EPOLLOUT;

epoll_ctl(epollFd, EPOLL_CTL_MOD, evs[i].data.fd, &evs[i]);

continue;

}

//返回信息给对应的客户端

if (evs[i].events & EPOLLOUT)

{

string wrtBuf = "hello,clients[i]ient " + to_string(i);

int nwrite = write(evs[i].data.fd, wrtBuf.c_str(), 1024);

if (nwrite == -1)

{

perror("write error");

close(evs[i].data.fd);

epoll_ctl(epollFd, EPOLL_CTL_DEL, evs[i].data.fd, &evs[i]);

continue;

}

evs[i].events = EPOLLIN;

epoll_ctl(epollFd, EPOLL_CTL_MOD, evs[i].data.fd, &evs[i]);

}

}

}

return 0;

}

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值