接著上文 "IO多路復用(一) Select、Poll、Epoll" ,接下來將演示一個TCP回射程式,源代碼來自於該博文https://www.cnblogs.com/Anker/p/3258674.html 博主的幾篇相關的文章,在這裡將其進行了整合,突出select、poll和epoll不同方 ...
接著上文IO多路復用(一)-- Select、Poll、Epoll,接下來將演示一個TCP回射程式,源代碼來自於該博文https://www.cnblogs.com/Anker/p/3258674.html 博主的幾篇相關的文章,在這裡將其進行了整合,突出select、poll和epoll不同方法之間的比較,但是代碼的結構相同,為了突出方法之間的差別,可能有的代碼改動的並不合理,實際中使用並非這麼寫。
程式邏輯
該程式的主要邏輯如下:
伺服器:
1. 開啟伺服器套接字
2. 將伺服器套接字加入要監聽的集合中(select的fd_set、poll的pollfd、epoll調用epoll_ctl)
3. 進入迴圈,調用IO多路復用的API函數(select/poll/epoll_create),如果有事件產生:
3.1. 伺服器套接字產生的事件,添加新的客戶端到監聽集合中
3.2. 客戶端套接字產生的事件,讀取數據,並立馬回傳給客戶端
客戶端:
1. 開啟客戶端套接字
2. 將客戶端套接字和標準輸入文件描述符加入要監聽的集合中(select的fd_set、poll的pollfd、epoll調用epoll_ctl)
3. 進入迴圈,調用IO多路復用的API函數(select/poll/epoll_create),如果有事件產生:
3.1. 客戶端套接字產生的事件,則讀取數據,將其輸出到控制台
3.2. 標準輸入文件描述符產生的事件,則讀取數據,將其通過客戶端套接字傳給伺服器
multiplexing.h
具體代碼如下,首先是頭文件
//multiplexing.h
#ifndef MULTIPLEXING_H
#define MULTIPLEXING_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <poll.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/epoll.h>
#include <unistd.h>
using namespace std;
#define MAXLINE 1024
class Multiplexing {
protected:
static const int DEFAULT_IO_MAX = 10; //預設的最大文件描述符
static const int INFTIM = -1;
int io_max; //記錄最大文件描述符
int listenfd; //監聽句柄
public:
Multiplexing() { this->io_max = DEFAULT_IO_MAX; }
Multiplexing(int max, int listenfd) { this->io_max = max; this->listenfd = listenfd; }
~Multiplexing() {}
virtual void server_do_multiplexing() = 0; //服務端io多路復用
virtual void client_do_multiplexing() = 0; //客戶端io多路復用
virtual void handle_client_msg() = 0; //處理客戶端消息
virtual bool accept_client_proc() = 0; //接收客戶端連接
virtual bool add_event(int confd, int event) = 0;
virtual int wait_event() = 0; // 等待事件
};
//-----------------select-------------------------
class MySelect : public Multiplexing {
private:
fd_set* allfds; //句柄集合
int* clifds; //客戶端集合
int maxfd; //記錄句柄的最大值
int cli_cnt; //客戶端個數
public:
MySelect() : Multiplexing() { allfds = NULL; clifds = NULL; maxfd = 0; cli_cnt = 0; }
MySelect(int max, int listenfd);
~MySelect() {
if (allfds) {
delete allfds;
allfds = NULL;
}
if (clifds) {
delete clifds;
clifds = NULL;
}
}
void server_do_multiplexing();
void client_do_multiplexing();
void handle_client_msg();
bool accept_client_proc();
bool add_event(int confd, int event);
bool init_event(); //每次調用select前都要重新設置文件描述符
int wait_event(); // 等待事件
};
//-----------------poll-------------------------
typedef struct pollfd Pollfd;
class MyPoll : public Multiplexing {
private:
Pollfd* clientfds; //poll中使用pollfd結構體指定一個被監視的文件描述符
int max_index; //記錄當前clientfds數組中使用的最大下標
public:
MyPoll() : Multiplexing() { clientfds = NULL; max_index = -1; }
MyPoll(int max, int listenfd);
~MyPoll() {
if (clientfds) {
delete clientfds;
clientfds = NULL;
}
}
void server_do_multiplexing();
void client_do_multiplexing();
void handle_client_msg();
bool accept_client_proc();
bool add_event(int confd, int event);
int wait_event(); // 等待事件
};
//-----------------epoll-------------------------
typedef struct epoll_event Epoll_event;
class MyEpoll : public Multiplexing {
private:
int epollfd; //epoll的句柄,用來管理多個文件描述符
Epoll_event *events; //事件數組
int nready; //在handle_client_msg函數中用到,傳給handle_client_msg函數的當前事件的個數
public:
MyEpoll() : Multiplexing() { events = NULL; epollfd = -1; }
MyEpoll(int max, int listenfd);
~MyEpoll() {
if (events) {
delete events;
events = NULL;
}
}
void server_do_multiplexing();
void client_do_multiplexing();
void handle_client_msg();
bool accept_client_proc();
bool add_event(int confd, int event);
bool delete_event(int confd, int event);
int wait_event(); // 等待事件
};
#endif // !MULTIPLEXING_H
multiplexing.cpp
然後是函數的實現,從各個函數的實現可以看到select、poll、epoll在使用過程中的區別,具體看代碼註釋
//multiplexing.cpp
#include "multiplexing.h"
//--------------------------select------------------
MySelect::MySelect(int max, int listenfd) : Multiplexing(max, listenfd) {
this->allfds = new fd_set[this->io_max];
this->clifds = new int[this->io_max];
if (NULL == this->allfds || NULL == this->clifds) {
perror("initialization failed!");
exit(-1);
}
this->cli_cnt = 0;
this->maxfd = 0;
//初始化客戶連接描述符
int i;
for (i = 0; i < io_max; i++) {
this->clifds[i] = -1;
}
}
void MySelect::server_do_multiplexing() {
int nready = 0;
int i = 0;
while (1) {
//重新初始化fd_set集合 -- 這裡與poll不同
init_event();
/*開始輪詢接收處理服務端和客戶端套接字*/
nready = wait_event();
if (-1 == nready) return;
if (0 == nready) continue;
if (FD_ISSET(this->listenfd, this->allfds)) {
/*監聽客戶端請求*/
if (!accept_client_proc()) //處理連接請求
continue;
if (--nready <= 0) //說明此時產生的事件個數小於等於1,所以不必再處理下麵的客戶連接信息
continue;
}
/*接受處理客戶端消息*/
handle_client_msg();
}
}
void MySelect::client_do_multiplexing() {
char sendline[MAXLINE], recvline[MAXLINE];
int n;
this->maxfd = -1;
int nready = -1;
if (this->io_max < 2) {
perror("please increase the max number of io!");
exit(1);
}
//添加連接描述符
if (!add_event(this->listenfd, -1)) {
perror("add event error!");
exit(1);
}
//添加標準輸入描述符
if (!add_event(STDIN_FILENO, -1)) {
perror("add event error!");
exit(1);
}
while (1) {
//重新初始化fd_set集合 -- 這裡與poll不同
init_event();
//等待事件產生
nready = wait_event();
if (-1 == nready) return;
if (0 == nready) continue;
//是否有客戶信息準備好
if (FD_ISSET(this->listenfd, this->allfds)) {
n = read(this->listenfd, recvline, MAXLINE);
if (n <= 0) {
fprintf(stderr, "client: server is closed.\n");
close(this->listenfd);
return;
}
write(STDOUT_FILENO, recvline, n);
}
//測試標準輸入是否準備好
if (FD_ISSET(STDIN_FILENO, this->allfds)) {
n = read(STDIN_FILENO, sendline, MAXLINE);
if (n <= 0) {
shutdown(this->listenfd, SHUT_WR);
continue;
}
write(this->listenfd, sendline, n);
}
}
}
bool MySelect::init_event() {
FD_ZERO(this->allfds); //重新設置文件描述符
/*添加監聽套接字*/
FD_SET(this->listenfd, this->allfds);
this->maxfd = this->listenfd;
int i;
int clifd = -1;
/*添加客戶端套接字*/
for (i = 0; i < this->cli_cnt; i++) {
clifd = this->clifds[i];
/*去除無效的客戶端句柄*/
if (clifd != -1) {
FD_SET(clifd, this->allfds);
}
this->maxfd = (clifd > this->maxfd ? clifd : this->maxfd);
}
}
bool MySelect::accept_client_proc() {
struct sockaddr_in cliaddr;
socklen_t cliaddrlen;
cliaddrlen = sizeof(cliaddr);
int connfd;
//接受新的連接
if ((connfd = accept(this->listenfd,
(struct sockaddr*)&cliaddr, &cliaddrlen)) == -1) {
if (errno == EINTR)
return false;
else {
perror("accept error:");
exit(1);
}
}
fprintf(stdout, "accept a new client: %s:%d\n",
inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);
return add_event(connfd, -1); //添加新的描述符
}
bool MySelect::add_event(int connfd, int event) { //在select中event並沒有作用
//將新的連接描述符添加到數組中
int i = 0;
for (i = 0; i < io_max; i++) {
if (this->clifds[i] < 0) {
this->clifds[i] = connfd;
this->cli_cnt++;
break;
}
}
if (i == io_max) {
fprintf(stderr, "too many clients.\n");
return false;
}
//將新的描述符添加到讀描述符集合中
FD_SET(connfd, this->allfds);
if (connfd > this->maxfd) this->maxfd = connfd;
return true;
}
void MySelect::handle_client_msg() {
int i = 0, n = 0;
int clifd;
char buf[MAXLINE];
memset(buf, 0, MAXLINE);
//處理信息
for (i = 0; i <= this->cli_cnt; i++) {
clifd = this->clifds[i];
if (clifd < 0) {
continue;
}
/*判斷客戶端套接字是否有數據*/
if (FD_ISSET(clifd, this->allfds)) {
//接收客戶端發送的信息
n = read(clifd, buf, MAXLINE);
if (n <= 0) {
/*n==0表示讀取完成,客戶都關閉套接字*/
FD_CLR(clifd, this->allfds);
close(clifd);
this->clifds[i] = -1;
continue;
}
//回寫數據
printf("recv buf is :%s\n", buf);
write(clifd, buf, n);
return;
}
}
}
int MySelect::wait_event() {
struct timeval tv;
/*每次調用select前都要重新設置文件描述符和時間,因為事件發生後,文件描述符和時間都被內核修改啦*/
tv.tv_sec = 30;
tv.tv_usec = 0;
/*開始輪詢接收處理服務端和客戶端套接字*/
int nready = select(this->maxfd + 1, this->allfds, NULL, NULL, &tv);
if (nready == -1) {
fprintf(stderr, "select error:%s.\n", strerror(errno));
}
if (nready == 0) {
fprintf(stdout, "select is timeout.\n");
}
return nready;
}
//-----------------poll-------------------------
MyPoll::MyPoll(int max, int listenfd) : Multiplexing(max, listenfd) {
this->clientfds = new Pollfd[this->io_max];
//初始化客戶連接描述符
int i;
for (i = 0; i < io_max; i++) {
this->clientfds[i].fd = -1;
}
this->max_index = -1;
}
void MyPoll::server_do_multiplexing() {
int sockfd;
int i;
int nready;
this->max_index = -1;
//註意:需要將監聽描述符添加在第一個位置
if (!add_event(this->listenfd, POLLIN)) {
perror("add listen event error!");
return;
}
//迴圈處理
while (1) {
//等待事件,獲取可用描述符的個數
nready = wait_event();
if (nready == -1) {
return;
}
if (nready == 0) {
continue;
}
//測試監聽描述符是否準備好
if (this->clientfds[0].revents & POLLIN) {
if (!accept_client_proc()) //處理連接請求
continue;
if (--nready <= 0) //說明此時產生的事件個數小於等於1,所以不必再處理下麵的客戶連接信息
continue;
}
//處理客戶連接
handle_client_msg();
}
}
void MyPoll::client_do_multiplexing() {
char sendline[MAXLINE], recvline[MAXLINE];
int n;
this->max_index = -1;
int nready = -1;
if (this->io_max < 2) {
perror("please increase the max number of io!");
exit(1);
}
//添加連接描述符
if (!add_event(this->listenfd, POLLIN)) {
perror("add event error!");
exit(1);
}
//添加標準輸入描述符
if (!add_event(STDIN_FILENO, POLLIN)) {
perror("add event error!");
exit(1);
}
while (1) {
//等待事件產生
nready = wait_event();
if (-1 == nready) return;
if (0 == nready) continue;
//是否有客戶信息準備好
if (this->clientfds[0].revents & POLLIN) {
n = read(this->listenfd, recvline, MAXLINE);
if (n <= 0) {
fprintf(stderr, "client: server is closed.\n");
close(this->listenfd);
return;
}
write(STDOUT_FILENO, recvline, n);
}
//測試標準輸入是否準備好
if (this->clientfds[1].revents & POLLIN) {
n = read(STDIN_FILENO, sendline, MAXLINE);
if (n <= 0) {
shutdown(this->listenfd, SHUT_WR);
continue;
}
write(this->listenfd, sendline, n);
}
}
}
bool MyPoll::accept_client_proc() {
struct sockaddr_in cliaddr;
socklen_t cliaddrlen;
cliaddrlen = sizeof(cliaddr);
int connfd;
//接受新的連接
if ((connfd = accept(this->listenfd,
(struct sockaddr*)&cliaddr, &cliaddrlen)) == -1) {
if (errno == EINTR)
return false;
else {
perror("accept error:");
exit(1);
}
}
fprintf(stdout, "accept a new client: %s:%d\n",
inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);
return add_event(connfd, POLLIN); //添加新的描述符
}
bool MyPoll::add_event(int connfd, int event) {
//將新的連接描述符添加到數組中
int i;
for (i = 0; i < io_max; i++) {
if (this->clientfds[i].fd < 0) {
this->clientfds[i].fd = connfd;
break;
}
}
if (i == io_max) {
fprintf(stderr, "too many clients.\n");
return false;
}
//將新的描述符添加到讀描述符集合中
this->clientfds[i].events = event;
if (i > this->max_index) this->max_index = i;
return true;
}
void MyPoll::handle_client_msg() {
int i, n;
char buf[MAXLINE];
memset(buf, 0, MAXLINE);
//處理信息
for (i = 1; i <= this->max_index; i++) {
if (this->clientfds[i].fd < 0)
continue;
//測試客戶描述符是否準備好
if (this->clientfds[i].revents & POLLIN) {
//接收客戶端發送的信息
n = read(this->clientfds[i].fd, buf, MAXLINE);
if (n <= 0) {
close(this->clientfds[i].fd);
this->clientfds[i].fd = -1;
continue;
}
write(STDOUT_FILENO, buf, n);
//向客戶端發送buf
write(this->clientfds[i].fd, buf, n);
}
}
}
int MyPoll::wait_event() {
/*開始輪詢接收處理服務端和客戶端套接字*/
int nready = nready = poll(this->clientfds, this->max_index + 1, INFTIM);
if (nready == -1) {
fprintf(stderr, "poll error:%s.\n", strerror(errno));
}
if (nready == 0) {
fprintf(stdout, "poll is timeout.\n");
}
return nready;
}
//------------------------epoll---------------------------
MyEpoll::MyEpoll(int max, int listenfd) : Multiplexing(max, listenfd) {
this->events = new Epoll_event[this->io_max];
//創建一個描述符
this->epollfd = epoll_create(this->io_max);
}
void MyEpoll::server_do_multiplexing() {
int i, fd;
int nready;
char buf[MAXLINE];
memset(buf, 0, MAXLINE);
//添加監聽描述符事件
if (!add_event(this->listenfd, EPOLLIN)) {
perror("add event error!");
exit(1);
}
while (1) {
//獲取已經準備好的描述符事件
nready = wait_event();
this->nready = nready;
if (-1 == nready) return;
if (0 == nready) continue;
//進行遍歷
/**這裡和poll、select都不同,因為並不能直接判斷監聽的事件是否產生,
所以需要一個for迴圈遍歷,這個for迴圈+判斷類似於poll中
if (FD_ISSET(this->listenfd, this->allfds))、
select中的if (this->clientfds[0].revents & POLLIN)
這裡只是儘量寫的跟poll、select中的結構類似,
但是實際代碼中,不應該這麼寫,這麼寫多加了一個for迴圈**/
for (i = 0; i < nready; i++) {
fd = events[i].data.fd;
//根據描述符的類型和事件類型進行處理
if ((fd == this->listenfd) && (events[i].events & EPOLLIN)) { //監聽事件
/*監聽客戶端請求*/
if (!accept_client_proc()) //處理連接請求
continue;
//說明此時產生的事件個數小於等於1,所以不必再處理下麵的客戶連接信息
if (--nready <= 0)
continue;
}
}
//處理客戶端事件
handle_client_msg();
}
close(epollfd);
}
bool MyEpoll::accept_client_proc() {
struct sockaddr_in cliaddr;
socklen_t cliaddrlen;
cliaddrlen = sizeof(cliaddr);
int connfd;
//接受新的連接
if ((connfd = accept(this->listenfd,
(struct sockaddr*)&cliaddr, &cliaddrlen)) == -1) {
if (errno == EINTR)
return false;
else {
perror("accept error:");
exit(1);
}
}
fprintf(stdout, "accept a new client: %s:%d\n",
inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);
return add_event(connfd, EPOLLIN); //添加新的描述符
}
void MyEpoll::client_do_multiplexing() {
char sendline[MAXLINE], recvline[MAXLINE];
int n;
int nready = -1;
int i, fd;
if (this->io_max < 2) {
perror("please increase the max number of io!");
exit(1);
}
//添加連接描述符
if (!add_event(this->listenfd, POLLIN)) {
perror("add event error!");
exit(1);
}
//添加標準輸入描述符
if (!add_event(STDIN_FILENO, POLLIN)) {
perror("add event error!");
exit(1);
}
while (1) {
//等待事件產生
nready = wait_event();
if (-1 == nready) return;
if (0 == nready) continue;
for (i = 0; i < nready; i++) {
fd = events[i].data.fd;
//根據描述符的類型和事件類型進行處理
if ((fd == this->listenfd) && (events[i].events & EPOLLIN)) { //監聽事件
n = read(this->listenfd, recvline, MAXLINE);
if (n <= 0) {
fprintf(stderr, "client: server is closed.\n");
close(this->listenfd);
return;
}
write(STDOUT_FILENO, recvline, n);
}
else {
n = read(STDIN_FILENO, sendline, MAXLINE);
if (n <= 0) {
shutdown(this->listenfd, SHUT_WR);
continue;
}
write(this->listenfd, sendline, n);
}
}
}
}
bool MyEpoll::add_event(int connfd, int event) {
//將新的描述符添加到讀描述符集合中
Epoll_event ev;
ev.events = event;
ev.data.fd = connfd;
return epoll_ctl(this->epollfd, EPOLL_CTL_ADD, connfd, &ev) == 0;
}
void MyEpoll::handle_client_msg() {
int i, fd;
char buf[MAXLINE];
memset(buf, 0, MAXLINE);
//處理信息
for (i = 0; i <= this->nready; i++) {
fd = this->events[i].data.fd;
if (fd == this->listenfd)
continue;
if (events[i].events & EPOLLIN) {
int n = read(fd, buf, MAXLINE);
if (n <= 0) {
perror("read error:");
close(fd);
delete_event(fd, EPOLLIN);
}
else {
write(STDOUT_FILENO, buf, n);
//向客戶端發送buf
write(fd, buf, strlen(buf));
}
}
}
}
int MyEpoll::wait_event() {
/*開始輪詢接收處理服務端和客戶端套接字*/
int nready = epoll_wait(this->epollfd, this->events, this->io_max, INFTIM);;
if (nready == -1) {
fprintf(stderr, "poll error:%s.\n", strerror(errno));
}
if (nready == 0) {
fprintf(stdout, "poll is timeout.\n");
}
return nready;
}
bool MyEpoll::delete_event(int fd, int state) {
Epoll_event ev;
ev.events = state;
ev.data.fd = fd;
return epoll_ctl(this->epollfd, EPOLL_CTL_DEL, fd, &ev) == 0;
}
伺服器代碼
#include "multiplexing.h"
#define IPADDRESS "127.0.0.1"
#define PORT 8787
#define LISTENQ 5
#define OPEN_MAX 1000
//函數聲明
//創建套接字併進行綁定
static int socket_bind(const char* ip, int port);
int main(int argc, char *argv[]) {
int listenfd = socket_bind(IPADDRESS, PORT);
if (listenfd < 0) {
perror("socket bind error");
return 0;
}
listen(listenfd, LISTENQ);
// 改動此處,調用不同的IO復用函數
MySelect mltp(OPEN_MAX, listenfd);
//MyPoll mltp(OPEN_MAX, listenfd);
//MyEpoll mltp(OPEN_MAX, listenfd);
mltp.server_do_multiplexing(); //處理服務端
return 0;
}
static int socket_bind(const char* ip, int port) {
int listenfd;
struct sockaddr_in servaddr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1) {
perror("socket error:");
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, ip, &servaddr.sin_addr);
servaddr.sin_port = htons(port);
if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) {
perror("bind error: ");
exit(1);
}
return listenfd;
}
客戶端代碼
#include "multiplexing.h"
#define MAXLINE 1024
#define IPADDRESS "127.0.0.1"
#define SERV_PORT 8787
static void handle_connection(int sockfd);
int main(int argc, char *argv[]) {
int sockfd;
struct sockaddr_in servaddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
inet_pton(AF_INET, IPADDRESS, &servaddr.sin_addr);
connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
// 改動此處,調用不同的IO復用函數
MySelect mltp(2, sockfd);
//MyPoll mltp(2, sockfd);
//MyEpoll mltp(2, sockfd);
poll.client_do_multiplexing(); // 處理客戶端
return 0;
}
運行結果
服務端:
客戶端:
完整代碼可以訪問筆者github:https://github.com/yearsj/ClientServerProject.git
參考資料
作者:yearsj
轉載請註明出處:https://www.cnblogs.com/yearsj/p/9647351.html
segmentfault對應博文:https://segmentfault.com/a/1190000016400430