结构体pollfd
struct pollfd
{
int fd; //file descriptor
short event; //event of interest on fd
short reven; //event that occurred on fd
}
每一个pollfd结构体指定了一个被监视的文件描述符,可以传递多个结构体,指示poll()监视多个文件描述符。每个结构体的events域是监视该文件描述符的事件掩码,由用户来设置这个域。revents域是文件描述符的操作结果事件掩码。内核在调用返回时设置这个域。events域中请求的任何事件都可能在revents域中返回。
pollfd结构体合法的事件
POLLIN有数据可读。
POLLRDNORM
有普通数据可读。
POLLRDBAND
有优先数据可读。
POLLPRI
有紧迫数据可读。
POLLOUT
写数据不会导致阻塞。
POLLWRNORM
写普通数据不会导致阻塞。
POLLWRBAND
写优先数据不会导致阻塞。
POLLMSG
SIGPOLL 消息可用。
此外,revents域中还可能返回下列事件:
POLLER
指定的文件描述符发生错误。
POLLHUP
指定的文件描述符挂起事件。
POLLNVAL
指定的文件描述符非法。
这些事件在events域中无意义,因为它们在合适的时候总是会从revents中返回。使用poll()和select()不一样,你不需要显式地请求异常情况报告。
POLLIN | POLLPRI等价于select()的读事件,POLLOUT |POLLWRBAND等价于select()的写事件。POLLIN等价于POLLRDNORM |POLLRDBAND,而POLLOUT则等价于POLLWRNORM。
例如,要同时监视一个文件描述符是否可读和可写,我们可以设置 events为POLLIN |POLLOUT。在poll返回时,我们可以检查revents中的标志,对应于文件描述符请求的events结构体。如果POLLIN事件被设置,则文件描述符可以被读取而不阻塞。如果POLLOUT被设置,则文件描述符可以写入而不导致阻塞。这些标志并不是互斥的:它们可能被同时设置,表示这个文件描述符的读取和写入操作都会正常返回而不阻塞。
timeout参数指定等待的毫秒数,无论I/O是否准备好,poll都会返回。timeout指定为负数值表示无限超时;timeout为0指示poll调用立即返回并列出准备好I/O的文件描述符,但并不等待其它的事件。这种情况下,poll()就像它的名字那样,一旦选举出来,立即返回。
函数原型
int poll(struct pollfd fdarray[], nfds_t nfds, int timeout);
poll返回值和错误代码
成功时,poll()返回结构体中revents域不为0的文件描述符个数;如果在超时前没有任何事件发生,poll()返回0;失败时,poll()返回-1,并设置errno为下列值之一:
EBADF
一个或多个结构体中指定的文件描述符无效。
EFAULT
fds指针指向的地址超出进程的地址空间。
EINTR
请求的事件之前产生一个信号,调用可以重新发起。
EINVAL
nfds参数超出PLIMIT_NOFILE值。
ENOMEM
可用内存不足,无法完成请求。
以上理论摘自http://www.cnblogs.com/nathan-1988/archive/2012/07/01/2571786.html 谢谢~~~
代码如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <poll.h>
#define MYPORT 6666 // the port users will be connecting to
#define BACKLOG 5 // how many pending connections queue will hold
#ifndef INFTIM
#define INFTIM -1
#endif
#define BUF_SIZE 1024
#define MAXLINE 100
#define MAXCLIENT 5
int fd_access[BACKLOG]; // accepted connection fd
int conn_amount; // current connection amount
int main(void)
{
int sock_fd, new_fd,len,conn_no; // listen on sock_fd, new connection on new_fd
struct sockaddr_in server_addr; // server address information
struct sockaddr_in client_addr; // connector's address information
socklen_t sin_size;
char buf[BUF_SIZE];
int result;
int i;
int maxsock=0;
int conn_fd;
struct pollfd client[MAXCLIENT];
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
server_addr.sin_family = AF_INET; // host byte order
server_addr.sin_port = htons(MYPORT); // short, network byte order
server_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(server_addr.sin_zero, '\0', sizeof(server_addr.sin_zero));
if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
{
perror("bind");
exit(1);
}
if (listen(sock_fd, BACKLOG) == -1)
{
perror("listen");
exit(1);
}
printf("listen port %d\n", MYPORT);
sin_size = sizeof(client_addr);
client[0].fd=sock_fd;
client[0].events=POLLIN;
for(i=1;i<MAXCLIENT;i++)
client[i].fd=-1;
while (1)
{
printf("helo\n");
result=poll(client,maxsock+1,INFTIM);
if(client[0].revents&POLLIN)
{
new_fd=accept(sock_fd,(struct sockaddr*)&client_addr,&sin_size);
for(i = 1; i < MAXCLIENT;++i)
{
if( client[i].fd < 0 )
{
client[i].fd = new_fd;
client[i].events = POLLIN;//POLLRDNORM;
break;
}
}
if( i == MAXCLIENT)
{
printf("too many clients");
exit(1);
}
if( i > maxsock )
maxsock = i;
if( --result<= 0 )
continue;
}
for(i = 1; i <= maxsock; i++)
{
if( (conn_fd = client[i].fd) < 0)
continue;
if(client[i].revents & (POLLIN | POLLERR))
{
if( (len = read(conn_fd, buf, MAXLINE)) < 0)
{
if( errno == ECONNRESET)
{
close(conn_fd);
client[i].fd = -1;
}
else
perror("read error");
}
else if(len == 0)
{
close(conn_fd);
client[i].fd = -1;
}
else
printf("%s\n",buf);
if(--conn_no <= 0)
break;
}
}
}
exit(0);
}
(END)
以上是服务器端
客户端代码见http://blog.youkuaiyun.com/xluren/article/details/8043484#t15