多路IO转接服务器也叫做多任务IO服务器。该类型服务器实现的主旨思想是,不在由应用程序自己监视连接,取而代之由内核替应用程序监视文件
主要使用方法有三种
1、select函数
1、select 能监听的文件描述符个数受限于FD_SETSIZE,一般为1024,单纯改变进程打开的文件描述符个数能改变select监听文件个数
2、解决1024以下客户端时使用select是很合适的,但如果连接客户端过多,select采用的是轮询模型,会大大降低服务器响应效率
#include<sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int select(int nfds,fd_set *readfds,fd_set *writefds,\
fd_set *exceptfds,struct timeval *timeout);
nfds:监控的文件描述符集里最大文件描述符加1,因为此参数会告诉内核检测前多少个文件描述符的状态
readfds:监控有读数据到到文件描述符集合,传入传出参数
writefds:监控写数据到达文件描述符集合,传入传出参数
exceptfds:监控异常发生文件描述符集合,如带外数据到达异常,传入传出参数
timeout:定时阻塞监控时间,3种情况
1、NULL,永远等下去
2、设置timeval,等待固定时间
3、设置timeval里时间均为0,检查描述字后立即返回,轮询
struct timeval{
long tv_sec; /*seconds*/
long tv_usec;/*microseconds*/
};
void FD_CLR(int fd,fd_set *set);//把文件描述符集合里fd清0
int FD_ISSET(int fd,fd_set *set);//测试文件描述符集合里fd是否置1
void FD_SET(int fd,fd_set*set);//把文件描述符集合里fd置1
void FD_ZERO(fd_set *set);//把文件描述符集合里所有位清0
server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAXLINE 80
#define SERV_PORT 6666
int main(argc,argv[])
{
int i,maxi,maxfd,listenfd,connfd,sockfd;
int nready,client[FD_SETSIZE];/*FD_SETSIZE 默认为1024*/
ssize_t n;
fd_set rset,allset;
char buf[MAXLINE];
char str[INET_ADDRSTRLEN]; /*#define INET_ADDRSTRLEN 16*/
socklen_t cliaddr_len;
struct sockaddr_in cliaddr,servaddr;
listenfd = socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
listen(listenfd,20);
maxfd = listenfd; //初始化
maxi = -1; //client[]的下标
for(i=0;i<FD_SETSIZE;i++)
{
client[i] = -1;//用-1初始化client[]
}
FD_ZERO(&allset);
FD_SET(listenfd,&allset);
for(;;){
rset = allset;
nready = select(maxfd+1,&rset,NULL,NULL,NULL);
if(nready < 0)
{
perr_exit("select error");}
if(FD_ISSET(listenfd,&rset)){
/*new clinet connection*/
cliaddr_len = sizeof(cliaddr);
connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&cliaddr_len);
printf("received from %s at PORT %d\n",
inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),
ntohs(cliaddr.sin_port);
for(i = 0;i<FD_SETSIZE;i++){
if(client[i]<0){
client[i] = connfd;/*保存accept返回的文件描述符到client[]里*/
break;
}
}
/*达到select能监控的文件个数上限1024*/
if(i == FD_SETSIZE){
fputs("two many clients\n",stderr);
exit(1);
}
FD_SET(connfd,&allset);/*添加一个新的文件描述符到监控信号集里*/
if(confd>maxfd)
{
maxfd = connfd;}/*select 第一个参数需要*/
if(i>maxi)
maxi = i; /*更新client[]最大下标值*/
if(--nready == 0)
{
continue;}/*如果没有更多的就绪文件描述符继续回到上面select阻塞监听,负责处理未处理完的就绪文件描述符*/
}
for(i=0;i<=maxi;i++){
/*检测哪个clients有数据就绪*/
if((sockfd = client[i])<0)
{
continue;}
if(FD_ISSET(sockfd,&rset)){
if((n==read(sockfd,buf,MAXLINE))==0){
close(sockfd); /*当client关闭连接时,服务器端也关闭对应连接*/
FD_CLR(sockfd,&allset);/*解除select监控此文件描述符*/
client[i] = -1;
}else{
int j;
for(j=0;j<n;j++)
{
buf[j] =toupper(buf[j]);}
write(sockfd,buf,n);
}
if(--nready == 0)
break;
}
}
}
close(listenfd);
return 0;
}
client.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include "wrap.h"
#define MAXLINE 80
#define SERV_PORT 6666
int main(int argc, char *argv[])
{
struct sockaddr_in servaddr;
char buf[MAXLINE];
int sockfd, n;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
servaddr.sin_port = htons(SERV_PORT);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
while (fgets(buf, MAXLINE, stdin) != NULL) {
write(sockfd, buf, strlen(buf));
n = read(sockfd, buf, MAXLINE);
if (n == 0)
printf("the other side has been closed.\n");
else
write(STDOUT_FILENO, buf, n);
}
close(sockfd);
return 0;
}
2 poll函数
#include <poll.h>
int poll(struct pollfd *fds,nfds_t nfds,int timeout);
struct pollfd{
int fd;/*文件描述符*/
short events;/*监控事件*/
short revents; /*监控事件中满足条件返回的事件*/
}
server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#incldue <poll.h>
#include <errno.h>
#define MAXLINE 80
#define SERV_PORT 5000
#define OPEN_MAX 1024
int main (int argc,char *argv[])
{
int i,j,maxi,listenfd,connfd,sockfd;
int nready;
ssize_t n;
char buff[MAXLINE],str[INET_ADDRSTRLEN];
socklen_t clilen;
struct pollfd client[OPEN_MAX];
struct sockaddr_in cliaddr,servaddr;
listenfd = socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
listen(listenfd,20);
client[0].fd = listenfd;
client[0].events = POLLRDNORM; /*listenfd 监听普通读事件*/
for(i = 1;i<OPEN_MAX;i++)
{
clent[i].fd = -1}/*用-1初始化client[]里剩下元素*/
maxi = 0;
for(;;)
{
nready = poll(client,maxi=1,-1); //阻塞
if(client[0].revents & POLLRDNORM){
clilen = sizeof(cliaddr);
connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
printf("received from %s at PORT %d\n",\
inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),\
ntohs(cliaddr.sin_port);)
for(i = 1;i<OPEN_MAX;i++){
if(client[i].fd < 0){
client[i].fd = connfd;/*找到client[]中空闲的位置,存放accept返回的connfd*/
break;
}
}
if(i == OPEN_MAX)
{
perr_exit("too many clients");}
client[i].events = POLLRONOM;/*设置刚刚返回的connfd,监控读事件*/
if(i > maxi)
{
maxi = i;}/*更新client[]中最大元素下标*/
if(--nready <= 0)
{
continus;}/*没有更多就绪事件时,继续回到poll阻塞*/
}
for(i = 1;i<maxi;i++){
/*检测client[]*/
if((sockfd = client[i].fd) < 0)
{
continue;}
if(client[i].revents & (POLLRDNORM | POLLERR)){
if((n=read(sockfd,buf,MAXLINE)) < 0){
if(errno == ECONNRESET){
/*当收到RST标志时*/
/*connection reset by client*/
printf("client[%d aborted connection\n",i);
close(sockfd);
client[i].fd = -1;
}else{
perr_exit("read error");
}
}else if (n == 0){
/*connection closed by client*/
printf("client[%d] closed connection\n",i);
close(sockfd);
client[i].fd = -1;
}else{
for(j=0;j<n;j++)
{
buf[j]=toupper(buf[j]);}
write(sockfd,buf,n);
}
if(--nready<=0)
{
break;}/*no more readable descriptors*/
}
}
}
return 0;
}
cleint.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#define MAXLINE 80
#define SERV_PORT 5000
int main (int argc,char *argv[])
{
struct sockaddr_in servaddr;
char buf[MAXLINE];
int sockfd,n;
sockfd = socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
int_pton(AF_INET,"127.0.0.1",&servaddr.sin_addr);
servaddr.sin_port = htons(SERV_PORT);
connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
while(fgets(buf,MAXLINE,stdin)!= NULL){
write(sockfd,buf,strlen(buf));
n = read(sockfd,buf,MAXLINE);
if(n == 0)
{
printf("the other side has benn closed.\n");}
else
{
write(STDOUT_FILENO,buf,n);}
}
close(sockfd);
return 0;
}
文章评论