目錄
1、select
1.1、fd_set結構體宣告
1.2、結構體timeval的宣告
1.3、select函式宣告
1.4、select實體
2、poll
2.1、poll的宣告
2.2、event引數
2.3、poll實體
3、epoll
3.1、epoll定義
3.2、 epoll實體
1、select
1.1、fd_set結構體宣告
/* /usr/include/x86_64-linux-gnu/bits/typesizes.h */
#define __FD_SETSIZE 1024
/* /usr/include/x86_64-linux-gnu/sys/select.h */
#define __NFDBITS (8 * (int) sizeof (__fd_mask))
/* /usr/include/x86_64-linux-gnu/sys/select.h */
/* The fd_set member is required to be an array of longs. */
typedef long int __fd_mask;
/* fd_set for select and pselect. */
typedef struct
{
/* XPG4.2 requires this member name. Otherwise avoid the name
from the global namespace. */
#ifdef __USE_XOPEN
__fd_mask fds_bits[__FD_SETSIZE / __NFDBITS];
# define __FDS_BITS(set) ((set)->fds_bits)
#else
__fd_mask __fds_bits[__FD_SETSIZE / __NFDBITS];
# define __FDS_BITS(set) ((set)->__fds_bits)
#endif
} fd_set;
1.2、結構體timeval的宣告
/* /usr/include/x86_64-linux-gnu/asm/posix_types_x32.h */
typedef long long __kernel_long_t;
/* /usr/include/asm-generic/posix_types.h */
typedef __kernel_long_t __kernel_time_t;
typedef __kernel_long_t __kernel_suseconds_t;
/* /usr/include/linux/time.h */
struct timeval {
__kernel_time_t tv_sec; /* seconds */
__kernel_suseconds_t tv_usec; /* microseconds */
};
1.3、select函式宣告
/* Check the first NFDS descriptors each in READFDS (if not NULL) for read
readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS
(if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out
after waiting the interval specified therein. Returns the number of ready
descriptors, or -1 for errors.
This function is a cancellation point and therefore not marked with
__THROW. */
/*
__nfds: 監控的檔案描述符集里最大檔案描述符加1,因為此引數會告訴內核檢測前多少個檔案描述符的狀態
__readfds: 被監控的讀資料到達的檔案描述符集合,傳入傳出引數
__writefds: 被監控的有寫資料到達的檔案描述符集合,傳入傳出引數
__exceptfds: 被監控的有例外資料到達的檔案描述符集合,傳入傳出引數
__restrict __timeout:
定時阻塞監控時間,3種情況
1.NULL,永遠等下去
2.設定timeval,等待固定時間
3.設定timeval里時間均為0,檢查描述字后立即回傳,輪詢
*/
extern int select (int __nfds, fd_set *__restrict __readfds,
fd_set *__restrict __writefds,
fd_set *__restrict __exceptfds,
struct timeval *__restrict __timeout);
/* /usr/include/x86_64-linux-gnu/bits/select.h */
#define __FD_SET(d, set) \
((void) (__FDS_BITS (set)[__FD_ELT (d)] |= __FD_MASK (d)))
#define __FD_CLR(d, set) \
((void) (__FDS_BITS (set)[__FD_ELT (d)] &= ~__FD_MASK (d)))
#define __FD_ISSET(d, set) \
((__FDS_BITS (set)[__FD_ELT (d)] & __FD_MASK (d)) != 0)
# define __FD_ZERO(fdsp) \
do { \
int __d0, __d1; \
__asm__ __volatile__ ("cld; rep; " __FD_ZERO_STOS \
: "=c" (__d0), "=D" (__d1) \
: "a" (0), "0" (sizeof (fd_set) \
/ sizeof (__fd_mask)), \
"1" (&__FDS_BITS (fdsp)[0]) \
: "memory"); \
} while (0)
/* /usr/include/x86_64-linux-gnu/sys/select.h */
#define FD_SET(fd, fdsetp) __FD_SET (fd, fdsetp) //把檔案描述符集合fdsetp里fd位置1
#define FD_CLR(fd, fdsetp) __FD_CLR (fd, fdsetp) //把檔案描述符集合fdsetp里fd位置為0
#define FD_ISSET(fd, fdsetp) __FD_ISSET (fd, fdsetp) //fd是否在fdset中
#define FD_ZERO(fdsetp) __FD_ZERO (fdsetp) //把檔案描述符集合里所有位清0
1.4、select實體
/*================================================================
* Copyright (C) 2021 baichao All rights reserved.
*
* 檔案名稱:select_server.cpp
* 創 建 者:baichao
* 創建日期:2021年02月02日
* 描 述:
*
================================================================*/
#include <iostream>
#include <netinet/in.h>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
class SelectServer
{
public:
SelectServer(std::string ip,int port)
{
memset(&serv_addr, 0, sizeof(serv_addr)); //每個位元組都用0填充
serv_addr.sin_family = AF_INET; //使用IPv4地址
serv_addr.sin_addr.s_addr = inet_addr(ip.c_str()); //具體的IP地址
serv_addr.sin_port = htons(port); //埠
serv_addr_len = sizeof(serv_addr);
listen_fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(listen_fd < 3)
{
std::cout<<"Socket error"<<std::endl;
return;
}
int opt = 1;
setsockopt(listen_fd,SOL_SOCKET,SO_REUSEADDR,&opt,0);
}
~SelectServer()
{
for (int fd = 0; fd <= max_fd; ++fd)
{
if (FD_ISSET(fd, &master_set))
{
close(fd);
}
}
}
int Bind()
{
if( bind(listen_fd,(struct sockaddr*) &serv_addr,serv_addr_len) == -1)
{
std::cout<<"Server bind failed"<<std::endl;
return -1;
};
std::cout<<"Server bind success"<<std::endl;
}
int Listen()
{
if( listen(listen_fd,20) == -1)
{
std::cout<<"Server listen failed"<<std::endl;
return -1;
};
std::cout<<"Server listen success"<<std::endl;
return 0;
}
int Accept()
{
struct sockaddr_in client_addr;
socklen_t client_addr_len =sizeof(client_addr);
int client_fd = accept(listen_fd,(struct sockaddr*) &client_addr,&client_addr_len);
if(client_fd < 0)
{
std::cout<<"Server accept failed"<<std::endl;
return -1;
}
FD_SET(client_fd,&master_set);
if(client_fd > max_fd)
max_fd = client_fd;
return client_fd;
}
int Run()
{
max_fd = listen_fd;
FD_ZERO(&master_set);
FD_SET(listen_fd,&master_set);
while(1)
{
FD_ZERO(&working_set);
memcpy(&working_set,&master_set,sizeof(master_set));
timeout.tv_sec = 20;
timeout.tv_usec = 0;
int num = select(max_fd + 1,&working_set,NULL,NULL,&timeout);
if(num < 0)
return -1;
int fd_isset_flag = FD_ISSET(listen_fd,&working_set);
std::cout<<"fd_isset_flag:"<<fd_isset_flag<<std::endl;
if(fd_isset_flag)
Accept(); //檢測到監聽的fd有訊息到達,所以創建一個新的客戶端fd
else
Recv(num);
}
}
int Recv(int num)
{
for(int fd = 0; fd <= max_fd; ++fd)
{
if(FD_ISSET(fd,&working_set))
{
bool isCloseConn = false;
int head_length;
recv(fd,&head_length,sizeof(head_length),0);
char *buffer = new char[head_length];
bzero(buffer,head_length);
int total = 0;
while(total < head_length)
{
int len = recv(fd,buffer+total,head_length - total,0);
if(len < 0)
{
std::cout<<"Recv error"<<std::endl;
isCloseConn = true;
break;
}
total = total + len;
}
std::cout<<"Message:"<<buffer<<std::endl;
delete buffer;
if (isCloseConn) // 當前這個連接有問題,關閉它
{
close(fd);
FD_CLR(fd, &master_set);
if (fd == max_fd) // 需要更新max_fd;
{
while (FD_ISSET(max_fd, &master_set) == false)
--max_fd;
}
}
}
}
return 0;
}
private:
struct sockaddr_in serv_addr;
socklen_t serv_addr_len;
int listen_fd;
int max_fd;
fd_set master_set;
fd_set working_set;
struct timeval timeout;
private:
int message_length;
};
int main()
{
SelectServer *selectServer = new SelectServer("127.0.0.1",11230);
selectServer->Bind();
selectServer->Listen();
selectServer->Run();
return 0;
}
/*================================================================
* Copyright (C) 2021 baichao All rights reserved.
*
* 檔案名稱:select_client.cpp
* 創 建 者:baichao
* 創建日期:2021年02月01日
* 描 述:
*
================================================================*/
#include<netinet/in.h> // sockaddr_in
#include<sys/types.h> // socket
#include<sys/socket.h> // socket
#include<arpa/inet.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#define BUFFER_SIZE 1024
class SelectClient
{
private:
struct sockaddr_in server_addr;
socklen_t server_addr_len;
int fd;
public:
SelectClient(std::string ip,int port);
~SelectClient();
void Connect();
void Send(std::string str);
std::string Recv();
};
SelectClient::SelectClient(std::string ip, int port)
{
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
if(inet_pton(AF_INET, ip.c_str(), &server_addr.sin_addr) == 0)
{
std::cout << "Server ip address error!";
exit(1);
}
server_addr.sin_port = htons(port);
server_addr_len = sizeof(server_addr);
// create socket
fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0)
{
std::cout << "Create socket failed!";
exit(1);
}
}
SelectClient::~SelectClient()
{
close(fd);
}
void SelectClient::Connect()
{
std::cout << "Connecting......" << std::endl;
if(connect(fd, (struct sockaddr*)&server_addr, server_addr_len) < 0)
{
std::cout << "Can not connect to cerver IP!";
exit(1);
}
std::cout << "Connect to server success." << std::endl;
}
void SelectClient::Send(std::string str)
{
int head_length = str.size()+1; // 注意這里需要+1
int ret1 = send(fd, &head_length, sizeof(head_length), 0);
int ret2 = send(fd, str.c_str(), head_length, 0);
if(ret1 < 0 || ret2 < 0)
{
std::cout << "Send message failed!";
exit(1);
}
}
int main()
{
SelectClient client("127.0.0.1",11230);
client.Connect();
while(1)
{
std::string msg;
getline(std::cin, msg);
if(msg == "exit")
break;
client.Send(msg);
}
return 0;
}


2、poll
2.1、poll的宣告
/* /usr/include/x86_64-linux-gnu/sys/poll.h */
/* Type used for the number of file descriptors. */
typedef unsigned long int nfds_t;
/* Data structure describing a polling request. */
struct pollfd
{
int fd; /* File descriptor to poll. */
short int events; /* Types of events poller cares about. */
short int revents; /* Types of events that actually occurred. */
};
/* Poll the file descriptors described by the NFDS structures starting at
FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
an event to occur; if TIMEOUT is -1, block until an event occurs.
Returns the number of file descriptors with events, zero if timed out,
or -1 for errors.
This function is a cancellation point and therefore not marked with
__THROW. */
/*
該poll()函式回傳__fds集合中就緒的讀、寫,或出錯的描述符數量,回傳0表示超時,回傳-1表示出錯
__fds:一個struct pollfd型別的陣列,用于存放需要檢測的socket描述符,并且呼叫poll函式后__fds陣列不會被清空
__nfds:__fds中描述符的總數量
timeout:呼叫poll函式阻塞的超時時間,單位毫秒
*/
extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout);
2.2、event引數
events域中請求的任何事件都可能在revents域中回傳,合法的事件如下:
/* Event types that can be polled for. These bits may be set in `events'
to indicate the interesting event types; they will appear in `revents'
to indicate the status of the file descriptor. */
#define POLLIN 0x001 /* There is data to read. */
#define POLLPRI 0x002 /* There is urgent data to read. */
#define POLLOUT 0x004 /* Writing now will not block. */
#if defined __USE_XOPEN || defined __USE_XOPEN2K8
/* These values are defined in XPG4.2. */
# define POLLRDNORM 0x040 /* Normal data may be read. */
# define POLLRDBAND 0x080 /* Priority data may be read. */
# define POLLWRNORM 0x100 /* Writing now will not block. */
# define POLLWRBAND 0x200 /* Priority data may be written. */
#endif
#ifdef __USE_GNU
/* These are extensions for Linux. */
# define POLLMSG 0x400
# define POLLREMOVE 0x1000
# define POLLRDHUP 0x2000
#endif
/* Event types always implicitly polled for. These bits need not be set in
`events', but they will appear in `revents' to indicate the status of
the file descriptor. */
#define POLLERR 0x008 /* Error condition. */
#define POLLHUP 0x010 /* Hung up. */
#define POLLNVAL 0x020 /* Invalid polling request. */
| 常量 | 說明 |
|---|---|
| POLLIN | 普通或優先級帶資料可讀 |
| POLLRDNORM | 普通資料可讀 |
| POLLRDBAND | 優先級帶資料可讀 |
| POLLPRI | 高優先級資料可讀 |
| POLLOUT | 普通資料可寫 |
| POLLWRNORM | 普通資料可寫 |
| POLLWRBAND | 優先級帶資料可寫 |
| POLLERR | 發生錯誤 |
| POLLHUP | 發生掛起 |
| POLLNVAL | 描述字不是一個打開的檔案 |
當需要監聽多個事件時,使用POLLIN | POLLRDNORM設定 events 域;當poll呼叫之后檢測某事件是否就緒時,fds[i].revents & POLLIN進行判斷,
2.3、poll實體
/*================================================================
* Copyright (C) 2021 baichao All rights reserved.
*
* 檔案名稱:poll_server.cpp
* 創 建 者:baichao
* 創建日期:2021年02月20日
* 描 述:
*
================================================================*/
#include <iostream>
#include <netinet/in.h>
#include <cstring>
#include <sys/socket.h>
#include <sys/poll.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXFD 1024
class PollServer
{
public:
PollServer(std::string ip,int port)
{
memset(&serv_addr, 0, sizeof(serv_addr)); //每個位元組都用0填充
serv_addr.sin_family = AF_INET; //使用IPv4地址
serv_addr.sin_addr.s_addr = inet_addr(ip.c_str()); //具體的IP地址
serv_addr.sin_port = htons(port); //埠
serv_addr_len = sizeof(serv_addr);
listen_fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(listen_fd < 3)
{
std::cout<<"Socket error"<<std::endl;
return;
}
int opt = 1;
setsockopt(listen_fd,SOL_SOCKET,SO_REUSEADDR,&opt,0);
}
~PollServer()
{
for (int i = 0; i < MAXFD; ++i)
{
if (fds[i].fd >= 0)
{
close(fds[i].fd);
}
}
}
int Bind()
{
if( bind(listen_fd,(struct sockaddr*) &serv_addr,serv_addr_len) == -1)
{
std::cout<<"Server bind failed"<<std::endl;
return -1;
};
std::cout<<"Server bind success"<<std::endl;
}
int Listen()
{
if( listen(listen_fd,20) == -1)
{
std::cout<<"Server listen failed"<<std::endl;
return -1;
};
std::cout<<"Server listen success"<<std::endl;
return 0;
}
int Accept()
{
struct sockaddr_in client_addr;
socklen_t client_addr_len =sizeof(client_addr);
int client_fd = accept(listen_fd,(struct sockaddr*) &client_addr,&client_addr_len);
if(client_fd < 0)
{
std::cout<<"Server accept failed"<<std::endl;
return -1;
}
int i ;
for( i = 1; i < MAXFD; ++i)
{
if(fds[i].fd < 0)
{
fds[i].fd = client_fd;
break;
}
}
if( i == MAXFD)
{
std::cout<<"client number exceeds maximum limit"<<std::endl;
return -1;
}
fds[i].events = POLLIN; //設定新描述符的讀事件
nfds = i > nfds ? i : nfds;
return client_fd;
}
int Run()
{
fds[0].fd = listen_fd;
fds[0].events = POLLIN;
nfds = 0;
for(int i = 1; i < MAXFD; ++i)
fds[i].fd = -1;
while(1)
{
int num = poll(fds,nfds+1,-1);
if(num < 0)
return -1;
if(fds[0].revents & POLLIN)
Accept(); //檢測到監聽的fd有訊息到達,所以創建一個新的客戶端fd
else
Recv(num);
}
}
int Recv(int num)
{
for(int i = 0; i <= MAXFD; ++i)
{
if(fds[i].revents & POLLIN)
{
bool isCloseConn = false;
int head_length;
recv(fds[i].fd,&head_length,sizeof(head_length),0);
char *buffer = new char[head_length];
bzero(buffer,head_length);
int total = 0;
while(total < head_length)
{
int len = recv(fds[i].fd,buffer+total,head_length - total,0);
if(len < 0)
{
std::cout<<"Recv error"<<std::endl;
isCloseConn = true;
break;
}
total = total + len;
}
std::cout<<"Message:"<<buffer<<std::endl;
delete buffer;
if (isCloseConn) // 當前這個連接有問題,關閉它
{
close(fds[i].fd);
fds[i].fd = -1;
}
}
}
return 0;
}
private:
struct sockaddr_in serv_addr;
socklen_t serv_addr_len;
int listen_fd;
struct pollfd fds[MAXFD];
int nfds;
private:
int message_length;
};
int main()
{
PollServer *pollServer = new PollServer("127.0.0.1",11230);
pollServer->Bind();
pollServer->Listen();
pollServer->Run();
return 0;
}


3、epoll
3.1、epoll定義
/* Valid opcodes ( "op" parameter ) to issue to epoll_ctl(). */
#define EPOLL_CTL_ADD 1 /* Add a file descriptor to the interface. */
#define EPOLL_CTL_DEL 2 /* Remove a file descriptor from the interface. */
#define EPOLL_CTL_MOD 3 /* Change file descriptor epoll_event structure. */
typedef union epoll_data
{
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t;
struct epoll_event
{
uint32_t events; /* Epoll events */
epoll_data_t data; /* User data variable */
} __EPOLL_PACKED;
/* 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(). */
extern int epoll_create (int __size) __THROW;
/* 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. */
extern int epoll_ctl (int __epfd, int __op, int __fd,
struct epoll_event *__event) __THROW;
/* 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).
This function is a cancellation point and therefore not marked with
__THROW. */
extern int epoll_wait (int __epfd, struct epoll_event *__events,
int __maxevents, int __timeout);
struct epoll_event中的events值可以為下面列舉中定義的值
enum EPOLL_EVENTS
{
EPOLLIN = 0x001, //表示對應的檔案描述符可以讀(包括對端SOCKET正常關閉);
#define EPOLLIN EPOLLIN
EPOLLPRI = 0x002, //表示對應的檔案描述符有緊急的資料可讀
#define EPOLLPRI EPOLLPRI
EPOLLOUT = 0x004, //表示對應的檔案描述符可以寫;
#define EPOLLOUT EPOLLOUT
EPOLLRDNORM = 0x040, //和 EPOLLIN 相等
#define EPOLLRDNORM EPOLLRDNORM
EPOLLRDBAND = 0x080, //優先讀取的資料帶(data band)
#define EPOLLRDBAND EPOLLRDBAND
EPOLLWRNORM = 0x100, //和 EPOLLOUT 相等
#define EPOLLWRNORM EPOLLWRNORM
EPOLLWRBAND = 0x200, //優先寫的資料帶(data band)
#define EPOLLWRBAND EPOLLWRBAND
EPOLLMSG = 0x400, //忽視
#define EPOLLMSG EPOLLMSG
EPOLLERR = 0x008, //assoc.fd有錯誤情況發生
#define EPOLLERR EPOLLERR
EPOLLHUP = 0x010, //assoc.fd發生掛起
#define EPOLLHUP EPOLLHUP
EPOLLRDHUP = 0x2000, //
#define EPOLLRDHUP EPOLLRDHUP
EPOLLWAKEUP = 1u << 29,
#define EPOLLWAKEUP EPOLLWAKEUP
EPOLLONESHOT = 1u << 30, //設定為 one-short 行為,一個事件(event)被拉出后,對應的fd在內部被禁用
#define EPOLLONESHOT EPOLLONESHOT
EPOLLET = 1u << 31
#define EPOLLET EPOLLET
};
3.2、 epoll實體
服務端代碼如下,客戶端代碼與select代碼一樣
/*================================================================
* Copyright (C) 2021 baichao All rights reserved.
*
* 檔案名稱:epoll_server.cpp
* 創 建 者:baichao
* 創建日期:2021年02月22日
* 描 述:
*
================================================================*/
#include <iostream>
#include <netinet/in.h>
#include <cstring>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
#define EPOLLSIZE 100
class EPollServer
{
public:
EPollServer(std::string ip,int port)
{
memset(&serv_addr, 0, sizeof(serv_addr)); //每個位元組都用0填充
serv_addr.sin_family = AF_INET; //使用IPv4地址
serv_addr.sin_addr.s_addr = inet_addr(ip.c_str()); //具體的IP地址
serv_addr.sin_port = htons(port); //埠
serv_addr_len = sizeof(serv_addr);
listen_fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(listen_fd < 3)
{
std::cout<<"Socket error"<<std::endl;
return;
}
int opt = 1;
setsockopt(listen_fd,SOL_SOCKET,SO_REUSEADDR,&opt,0);
}
~EPollServer()
{
close(epfd);
}
int Bind()
{
if( bind(listen_fd,(struct sockaddr*) &serv_addr,serv_addr_len) == -1)
{
std::cout<<"Server bind failed"<<std::endl;
return -1;
};
std::cout<<"Server bind success"<<std::endl;
}
int Listen()
{
if( listen(listen_fd,20) == -1)
{
std::cout<<"Server listen failed"<<std::endl;
return -1;
};
std::cout<<"Server listen success"<<std::endl;
return 0;
}
int Accept()
{
struct sockaddr_in client_addr;
socklen_t client_addr_len =sizeof(client_addr);
int client_fd = accept(listen_fd,(struct sockaddr*) &client_addr,&client_addr_len);
if(client_fd < 0)
{
std::cout<<"Server accept failed"<<std::endl;
return -1;
}
struct epoll_event event;
event.data.fd = client_fd;
event.events = EPOLLIN;
epoll_ctl(epfd,EPOLL_CTL_ADD,client_fd,&event);
return client_fd;
}
int Run()
{
epfd = epoll_create(1);
struct epoll_event event;
event.data.fd = listen_fd;
event.events = EPOLLIN;
epoll_ctl(epfd,EPOLL_CTL_ADD,listen_fd,&event);
while(1)
{
int num = epoll_wait(epfd,events,EPOLLSIZE,-1);
if(num < 0)
return -1;
for(int i = 0; i < num; ++i)
{
int fd = events[i].data.fd;
if((fd == listen_fd) && (events[i].events & EPOLLIN))
Accept(); //檢測到監聽的fd有訊息到達,所以創建一個新的客戶端fd
else if(events[i].events & EPOLLIN)
Recv(fd);
}
}
}
int Recv(int fd)
{
bool isCloseConn = false;
int head_length;
recv(fd,&head_length,sizeof(head_length),0);
char *buffer = new char[head_length];
bzero(buffer,head_length);
int total = 0;
while(total < head_length)
{
int len = recv(fd,buffer+total,head_length - total,0);
if(len < 0)
{
std::cout<<"Recv error"<<std::endl;
isCloseConn = true;
break;
}
total = total + len;
}
std::cout<<"Message:"<<buffer<<std::endl;
delete buffer;
if (isCloseConn) // 當前這個連接有問題,關閉它
{
close(fd);
struct epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN;
epoll_ctl(epfd,EPOLL_CTL_DEL,fd,&event); //洗掉出錯的fd
}
return 0;
}
private:
struct sockaddr_in serv_addr;
socklen_t serv_addr_len;
int listen_fd;
int epfd;
struct epoll_event events[EPOLLSIZE]; //epoll_wait回傳的就緒事件
private:
int message_length;
};
int main()
{
EPollServer *ePollServer = new EPollServer("127.0.0.1",11230);
ePollServer->Bind();
ePollServer->Listen();
ePollServer->Run();
return 0;
}


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/262625.html
標籤:其他
