生活随笔
收集整理的這篇文章主要介紹了
Linux网络编程——I/O复用之select详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://blog.csdn.net/lianghe_work/article/details/46506143
一、I/O復用概述
I/O復用概念:
解決進程或線程阻塞到某個 I/O 系統調用而出現的技術,使進程不阻塞于某個特定的 I/O 系統調
I/O復用使用的場合:
1.當客戶處理多個描述符(通常是交互式輸入、網絡套接字)時,必須使用I/O復用。
2.tcp服務器既要處理監聽套接字,又要處理已連接套接字,一般要使用I/O復用。
3.如果一個服務器既要處理tcp又要處理udp,一般要使用I/O復用。
4.如果一個服務器要處理多個服務或多個服務時,一般要使用I/O復用。
I/O復用常用函數:
select、poll
二、select()函數
select函數介紹:
int select(int?maxfd, fd_set *readset,?fd_set *writeset, fd_set *exceptset,?const struct timeval *timeout);
功能:輪詢掃描多個描述符中的任一描述符是否發生響應,或經過一段時間后喚醒參數:
| 參數 | 名稱 | 說明 |
| maxfd | 指定要檢測的描述符的范圍 | 所檢測描述符最大值+1
|
readset
| 可讀描述符集
| 監測該集合中的任意描述符是否有數據可讀
|
writeset
| 可寫描述符集
| 監測該集合中的任意描述符是否有數據可寫
|
exceptset
| 異常描述符集
| 監測該集合中的任意描述符是否發生異常
|
timeout
| 超時時間
| 超過規定時間后喚醒 |
返回值:
0:超時
-1:出錯
>0:準備好的文件描述符數量
頭文件:
#include <sys/select.h>#include <sys/time.h>
超時時間:
struct timeval{ long tv_sec; long tv_usec;}; struct timeval timeout;timeoout.tv_sec = 10;timeoout.tv_usec = 200000;
描述符集合的操作:
select()函數能對多個文件描述符進行監測,如果一個參數對應一個描述符,那么select函數的4個參數最多能監測4個文件描述,那他如何實現對多個文件描述符的監測的呢?
大家想一想文件描述符基本具有3種特性(讀、寫、異常),如果我們統一將監測可讀的描述符放入可讀集合(readset),監測可寫的描述符放入可寫集合(writeset),監測異常的描述符放入異常集合(exceptset)。然后將這3個集合傳給select函數,是不是就可監測多個描述符呢.
如何將某個描述符加入到特定的集合中呢?這時就需要了解下面的集合操作函數
/初始化描述符集void FD_ZERO(fd_set *fdset); void FD_SET(int fd, fd_set *fdset); void FD_CLR(int fd, fd_set *fdset); int FD_ISSET(int fd, fd_set *fdset);select()函數整體使用框架:
例子:檢測 0、4、5 描述符是否準備好讀
while(1){fd_set rset;FD_ZERO(&rset);FD_SET(0, &rset);FD_SET(4, &rset);FD_SET(5, &rset); if(select(5+1, &rset, NULL, NULL, NULL) > 0){ if(FD_ISSET(0, &rset)){ } if(FD_ISSET(4, &rset)){ } if(FD_ISSET(5, &rset)){ }}}
三、select函數的應用對比
我們通過udp同時收發的例子來說明select的妙處。
對于udp同時收發立馬想到的是一個線程收、另一個線程發,下面的代碼就是通過多線程來實現
#include <string.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/select.h>#include <sys/time.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <pthread.h> void *recv_thread(void* arg){ int udpfd = (int)arg; struct sockaddr_in addr;socklen_t addrlen = sizeof(addr); bzero(&addr,sizeof(addr)); while(1){ char buf[200] = ""; char ipbuf[16] = "";recvfrom(udpfd, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &addrlen);printf("\r\033[31m[%s]:\033[32m%s\n",inet_ntop(AF_INET,&addr.sin_addr,ipbuf,sizeof(ipbuf)),buf);write(1,"UdpQQ:",6);} return NULL;} int main(int argc,char *argv[]){ char buf[100] = ""; int udpfd = 0;pthread_t tid; struct sockaddr_in addr; struct sockaddr_in cliaddr; bzero(&addr,sizeof(addr));addr.sin_family = AF_INET;addr.sin_port = htons(8000);addr.sin_addr.s_addr = htonl(INADDR_ANY); bzero(&cliaddr,sizeof(cliaddr)); cliaddr.sin_family = AF_INET;cliaddr.sin_port = htons(8000); if( (udpfd = socket(AF_INET,SOCK_DGRAM, 0)) < 0){perror("socket error");exit(-1);} if(bind(udpfd, (struct sockaddr*)&addr, sizeof(addr)) < 0){perror("bind error");close(udpfd); exit(-1);} printf("input: \"sayto 192.168.221.X\" to sendmsg to somebody\n"); pthread_create(&tid, NULL, recv_thread, (void*)udpfd); printf("\033[32m"); fflush(stdout); while(1){ write(1,"UdpQQ:",6);fgets(buf, sizeof(buf), stdin); buf[strlen(buf) - 1] = '\0'; if(strncmp(buf, "sayto", 5) == 0){ char ipbuf[INET_ADDRSTRLEN] = "";inet_pton(AF_INET, buf+6, &cliaddr.sin_addr);printf("\rconnect %s successful!\n",inet_ntop(AF_INET,&cliaddr.sin_addr,ipbuf,sizeof(ipbuf))); continue;} else if(strncmp(buf, "exit",4) == 0){close(udpfd);exit(0);} sendto(udpfd, buf, strlen(buf),0,(struct sockaddr*)&cliaddr, sizeof(cliaddr));} return 0;} 運行結果:
用select來完成上述同樣的功能:
#include <string.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/select.h>#include <sys/time.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h> int main(int argc,char *argv[]){ int udpfd = 0; struct sockaddr_in saddr; struct sockaddr_in caddr; bzero(&saddr,sizeof(saddr));saddr.sin_family = AF_INET;saddr.sin_port = htons(8000);saddr.sin_addr.s_addr = htonl(INADDR_ANY); bzero(&caddr,sizeof(caddr));caddr.sin_family = AF_INET;caddr.sin_port = htons(8000); if( (udpfd = socket(AF_INET,SOCK_DGRAM, 0)) < 0){perror("socket error");exit(-1);} if(bind(udpfd, (struct sockaddr*)&saddr, sizeof(saddr)) != 0){perror("bind error");close(udpfd); exit(-1);} printf("input: \"sayto 192.168.220.X\" to sendmsg to somebody\033[32m\n"); while(1){ char buf[100]=""; fd_set rset; FD_ZERO(&rset); FD_SET(0, &rset);FD_SET(udpfd, &rset);write(1,"UdpQQ:",6); if(select(udpfd + 1, &rset, NULL, NULL, NULL) > 0){ if(FD_ISSET(0, &rset)){ fgets(buf, sizeof(buf), stdin);buf[strlen(buf) - 1] = '\0'; if(strncmp(buf, "sayto", 5) == 0){ char ipbuf[16] = "";inet_pton(AF_INET, buf+6, &caddr.sin_addr);printf("\rsay to %s\n",inet_ntop(AF_INET,&caddr.sin_addr,ipbuf,sizeof(ipbuf))); continue;} else if(strcmp(buf, "exit")==0){close(udpfd);exit(0);}sendto(udpfd, buf, strlen(buf),0,(struct sockaddr*)&caddr, sizeof(caddr));} if(FD_ISSET(udpfd, &rset)){ struct sockaddr_in addr; char ipbuf[INET_ADDRSTRLEN] = "";socklen_t addrlen = sizeof(addr); bzero(&addr,sizeof(addr)); recvfrom(udpfd, buf, 100, 0, (struct sockaddr*)&addr, &addrlen);printf("\r\033[31m[%s]:\033[32m%s\n",inet_ntop(AF_INET,&addr.sin_addr,ipbuf,sizeof(ipbuf)),buf);}}} return 0;} 運行結果:
總結
以上是生活随笔為你收集整理的Linux网络编程——I/O复用之select详解的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。