- 1,需要大量的运算
- 2,需要大量的内存空间
更多select和epoll比较,个人看了一些别人的贴子,可参考select和epoll比较
#include
#include
int select(int nfds, fd_set*readset, fd_set* writeset, fd_set*exceptset, const struct timeval * timeout);
// →成功时返回大于0的值,失败时返回-1。
select重点在服务端
void select_cs_connect(char* arg)
{
if (strcmp(arg, "s") == 0)//如果输入s,走服务端路线
{
select_server();
}
else
{
fputs("Please input your name:", stdout);
scanf("%s", name);
select_client();
}
}
//select函数
#include
#include
void select_server()
{
int serv_sock;
struct sockaddr_in serv_adr;
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if (serv_sock == -1)
{
printf("create socket error:%d %s\n", errno, strerror(errno));
return;
}
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_adr.sin_port = htons(9527);
if (bind(serv_sock, (struct sockaddr*)&serv_adr, sizeof(serv_adr)) == -1)
{
error_handling("thread server bind error");
printf("bind error msg:%d %s\n", errno, strerror(errno));
return;
}
if (listen(serv_sock, 5) == -1)
{
error_handling("thread server listen error");
printf("listen error msg:%d %s\n", errno, strerror(errno));
return;
}
printf("create server socket success!\n");
int clnt_sock;
struct sockaddr_in clnt_adr;
socklen_t clnt_adr_sz = sizeof(clnt_adr);
fd_set reads, copy_reads;
FD_ZERO(&reads);
FD_SET(serv_sock, &reads);
timeval timeout = {5, 5000};//设置5.5秒超时,结构体中,第二个参数为微秒结构0.5秒 = 5000000
int fd_max = serv_sock;
while (1)
{
copy_reads = reads;
int fd_num = select(fd_max + 1, ©_reads, NULL, NULL, &timeout);//套接字最大再加1,范围全部覆盖
if (fd_num == -1)
{
printf("select error msg:%d %s\n", errno, strerror(errno));
close(serv_sock);
return;
}
if (fd_num == 0) continue;
printf("fd_num is:%d\n",fd_num);
//轮询
for (int i = 0; i < fd_max + 1; i++)
{
if (FD_ISSET(i, ©_reads))
{
if (i == serv_sock) // connection request!
{
clnt_sock =
accept(serv_sock, (struct sockaddr*)&clnt_adr, &clnt_adr_sz);
FD_SET(clnt_sock, &reads);
if (fd_max < clnt_sock)
fd_max = clnt_sock;
printf("connected client: %d \n", clnt_sock);
}
else // read message!
{
char buf[256] = "";
ssize_t str_len = read(i, buf, BUF_SIZE);
if (str_len == 0) // close request!
{
FD_CLR(i, &reads);
close(i);
printf("closed client: %d \n", i);
}
else
{
write(i, buf, str_len); // echo!
}
}
}
}
}
close(serv_sock);
}
// 客户端 发送消息
char name[64] = "[MOON]";
void* client_send_msg(void* arg)
{
pthread_detach(pthread_self());
int clnt_sock = *(int*)arg;//取出当前线程的socket
char msg[256] = "";
char buffer[1024];
while (1)
{
memset(buffer, 0, sizeof(buffer));
fgets(msg, sizeof(msg), stdin);//对文件的标准输入流操作 读取buffer的256字节
if (strcmp(msg, "q\n") == 0 || (strcmp(msg, "Q\n") == 0)) {
break;
}
if (strcmp(msg, "") == 0)
{
continue;
}
snprintf(buffer, sizeof(buffer), "%s: %s", name, msg);
size_t len = strlen(buffer);
size_t send_len = 0;
//当数据量很大时,并不能一次把所有数据全部发送完,因此需要分包发送
while (send_len < len)
{
ssize_t ret = write(clnt_sock, buffer + send_len, len - send_len);//send_len 记录分包的标记
if (ret <= 0) {//连接出了问题
fputs("may be connect newwork failed,make client write failed!\n", stdout);
break;
}
send_len += (size_t)ret;
}
};
sem_post(&semid);
pthread_exit(NULL);
}
//客户端-接收消息
void* client_recv_msg(void* arg)
{
pthread_detach(pthread_self());
int clnt_sock = *(int*)arg;//取出当前线程的socket
char buffer[1024] = "";
while (1)
{
size_t ret = read(clnt_sock, buffer, sizeof(buffer));
if (ret <= 0) {//连接出了问题
fputs("client read failed!\n", stdout);
break;
}
fputs(buffer, stdout);
memset(buffer, 0, ret);//处理完消息及时重置内存
};
sem_post(&semid);
pthread_exit(NULL);
}
void select_client()
{
struct sockaddr_in clnt_adr;
socklen_t clnt_adr_sz = sizeof(clnt_adr);
int clnt_sock = socket(PF_INET, SOCK_STREAM, 0);
memset(&clnt_adr, 0, clnt_adr_sz);
clnt_adr.sin_family = AF_INET;
clnt_adr.sin_addr.s_addr = inet_addr("127.0.0.1");
clnt_adr.sin_port = htons(9527);
if (connect(clnt_sock, (struct sockaddr*)&clnt_adr, clnt_adr_sz) == -1)
{
printf("connect error msg:%d %s\n", errno, strerror(errno));
return;
}
pthread_t thread_send, thread_recv;
sem_init(&semid, 0, -1);
pthread_create(&thread_send, NULL, client_send_msg, &clnt_sock);//消息clnt_sock为局部变量
pthread_create(&thread_recv, NULL, client_recv_msg, &clnt_sock);//消息clnt_sock为局部变量
sem_wait(&semid);
close(clnt_sock);
}
epoll这个是Linux独有的函数
。Epoll的三大函数:epoll_create,epoll_wait, epoll_ctl
#include
int epoll_create(int size);
//→成功时返回epoll文件描述符,失败时返回-1。
#include
int epoll_ctl(int epfd, int op, int fd, struct epoll_event* event);
//→成功时返回0,失败时返回-1。
#include
int epoll_wait(int epfd, struct epoll_event*events,int maxevents,int timeout);
//→成功时返回发生事件的文件描述符数,失败时返回-1。
// epoll模型
#include
void epoll_server()
{
int serv_sock;
struct sockaddr_in serv_adr;
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if (serv_sock == -1)
{
printf("create socket error:%d %s\n", errno, strerror(errno));
close(serv_sock);
return;
}
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_adr.sin_port = htons(9527);
if (bind(serv_sock, (struct sockaddr*)&serv_adr, sizeof(serv_adr)) == -1)
{
error_handling("thread server bind error");
printf("bind error msg:%d %s\n", errno, strerror(errno));
close(serv_sock);
return;
}
if (listen(serv_sock, 5) == -1)
{
error_handling("thread server listen error");
printf("listen error msg:%d %s\n", errno, strerror(errno));
close(serv_sock);
return;
}
printf("create server socket success!\n");
int clnt_sock;
struct sockaddr_in clnt_adr;
socklen_t clnt_adr_sz = sizeof(clnt_adr);
epoll_event event;
int event_cnt;
int epfd = epoll_create(1);
if (epfd == -1)
{
printf("epoll_create error msg:%d %s\n", errno, strerror(errno));
close(serv_sock);
return;
}
epoll_event* all_events = new epoll_event[100];
event.events = EPOLLIN;
event.data.fd = serv_sock;
epoll_ctl(epfd, EPOLL_CTL_ADD, serv_sock, &event);
while (true)
{
event_cnt = epoll_wait(epfd, all_events, 100, 1000);//1000:等待1秒
if (event_cnt == -1)
printf("epoll_wait error msg:%d %s\n", errno, strerror(errno));
break;
if (event_cnt == 0)continue;
for (int i = 0; i < event_cnt; i++)
{
int event_data_fd = all_events[i].data.fd;
if (event_data_fd == serv_sock) {
clnt_sock = accept(serv_sock, (sockaddr*)&clnt_adr, &clnt_adr_sz);
event.events = EPOLLIN;
event.data.fd = clnt_sock;
epoll_ctl(epfd, EPOLL_CTL_ADD, clnt_sock, &event);
printf("client is connect success! %d\n", clnt_sock);
}
else
{
char buf[256] = "";
ssize_t str_len = read(event_data_fd, buf, sizeof(buf));
if (str_len <= 0) // close request!
{
epoll_ctl(epfd, EPOLL_CTL_DEL, event_data_fd, NULL);
close(event_data_fd);
printf("closed client: %d \n", i);
}
else
{
write(event_data_fd, buf, str_len); // echo!
}
}
}
}
delete[] all_events;
close(serv_sock);
close(epfd);
}
void epoll_cs_connect(char* arg)
{
if (strcmp(arg, "s") == 0)//如果输入s,走服务端路线
{
epoll_server();
}
else
{
fputs("Please input your name:", stdout);
scanf("%s", name);
select_client();
}
}
条件触发(level-triggered,也被称为水平触发)LT:
只要满足条件,就触发一个事件(只要有数据没有被获取,内核就不断通知你)
边缘触发(edge-triggered)ET:
每当状态变化时,触发一个事件
“举个读socket的例子,假定经过长时间的沉默后,现在来了100个字节,这时无论边缘触发和条件触发都会产生一个通知应用程序可读。应用程序读了50个字节,然后重新调用api等待io事件。
这时水平触发的api会因为还有50个字节可读从而立即返回用户一个read ready notification。
而边缘触发的api会因为可读这个状态没有发生变化而陷入长期等待。 因此在使用边缘触发的api时,要注意每次都要读到socket返回EWOULDBLOCK为止,否则这个socket就算废了。而使用条件触发的api 时,如果应用程序不需要写就不要关注socket可写的事件,否则就会无限次的立即返回一个write ready notification。
select属于典型的条件触发。
epoll中如果设置events为EPOLLET则为边缘触发
边缘触发:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUF_SIZE 4
#define EPOLL_SIZE 50
void setnonblockingmode(int fd);
void error_handling(char *buf);
int main(int argc, char *argv[])
{
int serv_sock, clnt_sock;
struct sockaddr_in serv_adr, clnt_adr;
socklen_t adr_sz;
int str_len, i;
char buf[BUF_SIZE];
struct epoll_event *ep_events;
struct epoll_event event;
int epfd, event_cnt;
if(argc!=2) {
printf("Usage : %s \n" , argv[0]);
exit(1);
}
serv_sock=socket(PF_INET, SOCK_STREAM, 0);
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family=AF_INET;
serv_adr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_adr.sin_port=htons(atoi(argv[1]));
if(bind(serv_sock, (struct sockaddr*) &serv_adr, sizeof(serv_adr))==-1)
error_handling("bind() error");
if(listen(serv_sock, 5)==-1)
error_handling("listen() error");
epfd=epoll_create(EPOLL_SIZE);
ep_events=malloc(sizeof(struct epoll_event)*EPOLL_SIZE);
setnonblockingmode(serv_sock);
event.events=EPOLLIN;
event.data.fd=serv_sock;
epoll_ctl(epfd, EPOLL_CTL_ADD, serv_sock, &event);
while(1)
{
event_cnt=epoll_wait(epfd, ep_events, EPOLL_SIZE, -1);
if(event_cnt==-1)
{
puts("epoll_wait() error");
break;
}
puts("return epoll_wait");
for(i=0; i<event_cnt; i++)
{
if(ep_events[i].data.fd==serv_sock)
{
adr_sz=sizeof(clnt_adr);
clnt_sock=accept(serv_sock, (struct sockaddr*)&clnt_adr, &adr_sz);
setnonblockingmode(clnt_sock);
event.events=EPOLLIN|EPOLLET;
event.data.fd=clnt_sock;
epoll_ctl(epfd, EPOLL_CTL_ADD, clnt_sock, &event);
printf("connected client: %d \n", clnt_sock);
}
else
{
while(1)
{
str_len=read(ep_events[i].data.fd, buf, BUF_SIZE);
if(str_len==0) // close request!
{
epoll_ctl(epfd, EPOLL_CTL_DEL, ep_events[i].data.fd, NULL);
close(ep_events[i].data.fd);
printf("closed client: %d \n", ep_events[i].data.fd);
break;
}
else if(str_len<0)
{
if(errno==EAGAIN)
break;
}
else
{
write(ep_events[i].data.fd, buf, str_len); // echo!
}
}
}
}
}
close(serv_sock);
close(epfd);
return 0;
}
void setnonblockingmode(int fd)
{
int flag=fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flag|O_NONBLOCK);
}
void error_handling(char *buf)
{
fputs(buf, stderr);
fputc('\n', stderr);
exit(1);
}