select (Unix)
select
int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* errorfds, struct timeval* timeout);
描述 | |
---|---|
nfds | sets |
readfds | fd_set 类型, |
writefds | fd_set 类型, |
errorfds | fd_set 类型, |
timeout | struct timeval 类型 |
为了维护fd_set类型
这个函数 将 返 回 描述符 集 的 个数, 如果超 时返 回 为0,错误则返回 -1。
- select(2)
- poll(2)
select与 epoll的 区 别
[编辑]epoll | select | |
---|---|---|
epoll |
select | |
结构 |
typedef union epoll_data {
void *ptr;
int fd;
__uint32_t u32;
__uint64_t u64;
} epoll_data_t;
struct epoll_event {
__uint32_t events; // epoll 监听 |
struct timeval{
long tv_sec;//second
long tv_usec;//minisecond
}
typedef struct fd_set {
u_int fd_count;
int fd_array[FD_SETSIZE];
} //fd_array |
EPOLLIN : |
fd_set readfds, writefds, exceptionfds
| |
运行 |
边沿触发 (ET)、 |
|
运行过程 | int fd = epoll_create(1); // 创建 |
struct timeval tv;
fd_set rfds;
tv={5,0}; // 设置 |
优点 | 1)epoll_wait |
|
说明:
示 例
[编辑]#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/select.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#define PORT "9421"
/* function prototypes */
void die(const char*);
int main(int argc, char **argv)
{
int sockfd, new, maxfd, on = 1, nready, i;
struct addrinfo *res0, *res, hints;
char buffer[BUFSIZ];
fd_set master, readfds;
ssize_t nbytes;
(void)memset(&hints, '\0', sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
if(-1 == (getaddrinfo(NULL, PORT, &hints, &res0)))
die("getaddrinfo()");
for(res = res0; res; res = res->ai_next)
{
if(-1 == (sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)))
{
perror("socket()");
continue;
}
if(-1 == (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(int))))
{
perror("setsockopt()");
continue;
}
if(-1 == (bind(sockfd, res->ai_addr, res->ai_addrlen)))
{
perror("bind");
continue;
}
break;
}
if(-1 == sockfd)
exit(EXIT_FAILURE);
freeaddrinfo(res0);
if(-1 == (listen(sockfd, 32)))
die("listen()");
if(-1 == (fcntl(sockfd, F_SETFD, O_NONBLOCK)))
die("fcntl()");
FD_ZERO(&master);
FD_ZERO(&readfds);
FD_SET(sockfd, &master);
maxfd = sockfd;
while(1)
{
memcpy(&readfds, &master, sizeof(master));
(void)printf("running select()\n");
if(-1 == (nready = select(maxfd+1, &readfds, NULL, NULL, NULL)))
die("select()");
(void)printf("Number of ready descriptor: %d\n", nready);
for(i=0; i<=maxfd && nready>0; i++)
{
if(FD_ISSET(i, &readfds))
{
nready--;
if(i == sockfd)
{
(void)printf("Trying to accept() new connection(s)\n");
if(-1 == (new = accept(sockfd, NULL, NULL)))
{
if(EWOULDBLOCK != errno)
die("accept()");
break;
}
else
{
if(-1 == (fcntl(new, F_SETFD, O_NONBLOCK)))
die("fcntl()");
FD_SET(new, &master);
if(maxfd < new)
maxfd = new;
}
}
else
{
(void)printf("recv() data from one of descriptors(s)\n");
nbytes = recv(i, buffer, sizeof(buffer), 0);
if(nbytes <= 0)
{
if(EWOULDBLOCK != errno)
die("recv()");
break;
}
buffer[nbytes] = '\0';
printf("%s", buffer);
(void)printf("%zi bytes received.\n", nbytes);
close(i);
FD_CLR(i, &master);
}
}
}
}
return 0;
}
void die(const char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}