多路I/O轉接服務器

freeline-x 2022-01-08 03:29:02 阅读数:940

多路

多路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;
}
版权声明:本文为[freeline-x]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201080329017459.html