歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux網絡 >> Linux網絡編程:I/O復用之select詳解

Linux網絡編程:I/O復用之select詳解

日期:2017/3/1 9:05:38   编辑:Linux網絡

一、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;//微秒
};

//比如等待10.2秒
struct timeval timeout;
timeoout.tv_sec = 10;
timeoout.tv_usec = 200000;

//將select函數的timeout參數設置為NULL則永遠等待

描述符集合的操作:

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;//創建一個描述符集rset
FD_ZERO(&rset);//對描述符集rset清零
FD_SET(0, &rset);//將描述符0加入到描述符集rset中
FD_SET(4, &rset);//將描述符4加入到描述符集rset中
FD_SET(5, &rset);//將描述符5加入到描述符集rset中

if(select(5+1, &rset, NULL, NULL, NULL) > 0)
{
if(FD_ISSET(0, &rset))
{
//描述符0可讀及相應的處理代碼
}

if(FD_ISSET(4, &rset))
{
//描述符4可讀及相應的處理代碼
}
if(FD_ISSET(5, &rset))
{
//描述符5可讀及相應的處理代碼
}
}
}

三、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);//1 表示標准輸出
fgets(buf, sizeof(buf), stdin); //等��輸入
buf[strlen(buf) - 1] = '\0'; //確保輸入的最後一位是'\0'
if(strncmp(buf, "sayto", 5) == 0)
{
char ipbuf[INET_ADDRSTRLEN] = "";
inet_pton(AF_INET, buf+6, &cliaddr.sin_addr);//給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); //文件描述符聚合變量清0
FD_SET(0, &rset);//將標准輸入添加到文件描述符聚合變量中
FD_SET(udpfd, &rset);//將udpfd添加到文件描述符聚合變量中
write(1,"UdpQQ:",6);

if(select(udpfd + 1, &rset, NULL, NULL, NULL) > 0)
{
if(FD_ISSET(0, &rset))//測試0是否可讀寫
{
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);//給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))//測試udpfd是否可讀寫
{
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公社資源站下載:

------------------------------------------分割線------------------------------------------

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2017年資料/2月/13日/Linux網絡編程:IO復用之select詳解/

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

------------------------------------------分割線------------------------------------------

Copyright © Linux教程網 All Rights Reserved