I/O复用使得程序能同时监听多个文件描述符。其适用场合为:
- 客户端:同时处理多个socket,同时处理用户输入和为网络链接。
- 服务端:同时处理监听、链接socket,同时监听多个端口,同时处理TCP和UDP请求。
注意:I/O复用本身是阻塞的,当多个描述符同时就绪时,如果不使用多进程/多线程,那么就会按顺序依次处理。
一、 select系统调用
在一段时间内,监听用户感兴趣的文件描述符上的可读、可写和异常事件。
1 函数概述
#include <sys/select.h>
int select(int nfds, fd_set* read_fds, fd_set* write_fds, fd_set* exception_fds, struct timeval* timeout);
//nfds 指定被监听文件描述符的总数,通常被设为所监听的最大文件描述符值加1。
//read_fds、write_fds、exception_fds 分别表示可读、可写和异常事件所对应的文件描述符集。
//timeout 设置select函数的超时时间。
//调用成功返回就绪文件描述符总数,失败返回-1。
2 文件描述符的就绪条件
1) 可读:
socket内核接收缓冲区的字节数大于或等于低水位标记SO_RCVLOWAT.
socket通信的对端关闭了链接(此时对socket的读将返回0).
监听socket上有新的链接请求.
socket上有未处理的错误.
2) 可写:
socket内核发送缓冲区的可用字节数大于或等于其低水位标记SO_SNDLOWAT.
socket上的写端被关闭(向被关闭的写端·执行写操作,将触发SIGPIPE信号).
socket使用非阻塞connect连接成功或失败之后.
socket上有未处理的错误.
3)异常
socket上接收到带外数据.
3 处理带外数据
//socket上接收到普通数据和带外数据都将使得socket返回,但是socket会处于不同的就绪状态(可读状态/异常状态)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#inlcude <string.h>
#include <fcntl.h>
#include <stdlib.h>
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
if(argc != 3){
printf("Usage: %s IP_ADDRESS PORT_NUMBER\n", basename(argv[0]));
return 1;
}
const char* ip = argv[1];
int port = atoi(argv[2]);
struct sockaddr_in address;//填充地址内容
address.sin_family = AF_INET;
inet_pton(AF_INET, ip, &address.sin_addr);
address.sin_port = htons(port);
int listenfd = socket(PF_INET, SOCK_STREAM, 0);//创建套接字
assert(listenfd >= 0);
int ret = bind(listenfd, (struct sockaddr*)&address, sizeof(address));//绑定
assert(ret != -1);
//简单例子,服务器只接受一个连接,然后自己进入while循环
ret = listen(listenfd, 1);
assert(ret != -1);
struct sockaddr_in client;
socklen_t client_length = sizeof(client);
int connfd = accept(listenfd, (struct sockaddr*)&client, &length_client);//接收
if(connfd < 0){
printf("error is: %d\n", errno);
close(listenfd);
return 1;
}
char buf[BUFFER_SIZE];
fd_set read_fds;
fd_set exception_fds;
FD_ZERO(&read_fds);
FD_ZERO(&exception_fds);
while(1){
memset(buf, '\0', BUFFER_SIZE);
//内核会动态的修改参数,所以在每次调用select之前,都要重置参数.
FD_SET(connfd, &read_fds);
FD_SET(connfd, &exception_fds);
ret = select(connfd+1, &read_fds, NULL, &exception_fds, NULL);
if(ret < 0){
printf("selection failure.\n");
break;
}
if(FD_ISSET(connfd, &read_fds)){
ret = recv(connfd, buf, BUFFER_SIZE-1, 0);
if(ret <= 0){
printf("recv normal data failure.\n");
break;
}else{
printf("get %d bytes of normal data: %s\n", ret, buf);
}
}else if(FD_ISSET(connfd, &exception_fds)){
ret = recv(connfd, buf, BUFFER_SIZE-1, MSG_OOB);
if(ret <= 0){
printf("recv oob_data failure.\n");
break;
}else{
printf("get %d bytes oob_data: %s\n", ret, buf);
}
}
}
close(connfd);
close(listenfd);
return 0;
}