linux poll 学习

一、poll介绍

函数原型:

#include <poll.h>
int poll(struct pollfd *fds, nfds_t nfds, int timeout);

struct pollfd定义:

struct pollfd {
int fd; /* file descriptor */
short events; /* requested events */
short revents; /* returned events */
};

fd:文件描述符

events:关心的事件,输入参数

revents:产生的事件,输出参数

事件类型:

POLLIN    普通或优先级带数据可读
POLLRDNORM 普通数据可读
POLLRDBAND 优先级带数据可读
POLLPRI 高优先级数据可读
POLLOUT 普通数据可写
POLLWRNORM 普通数据可写
POLLWRBAND 优先级带数据可写
POLLERR 发生错误
POLLHUP 发生挂起
POLLNVAL 描述字不是一个打开的文件

关于poll的具体用法,请见下面示例。

二、poll使用

示例1:回显服务器

/*******************************************************************************
* File Name : poll.cpp
* Author : zjw
* Email : emp3XzA3MjJAMTYzLmNvbQo= (base64 encode)
* Create Time : 2015年07月17日 星期五 09时42分11秒
*******************************************************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <poll.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h> #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include <cstring>
using namespace std; const int SERVER_PORT = ;
const int EVENT_SIZE = ;
const int RECV_SIZE = ;
const int SEND_SIZE = ; int main(int argc, char **argv)
{
int server = socket(AF_INET, SOCK_STREAM, );
struct sockaddr_in addrServer;
bzero(&addrServer, sizeof(addrServer));
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(SERVER_PORT);
addrServer.sin_addr.s_addr = INADDR_ANY; if (bind(server, (struct sockaddr*)&addrServer, sizeof(addrServer)))
{
perror("bind failed,");
return -;
} listen(server, );
cout << "listening on port:" << SERVER_PORT << " ..." << endl; struct pollfd fds[EVENT_SIZE] = {{, , }};
fds[server].fd = server;
fds[server].events = POLLIN; int ret = ;
char recvBuf[RECV_SIZE + ];
char sendBuf[SEND_SIZE + ];
while ()
{
if ((ret = poll(fds, EVENT_SIZE, )) < )
{
perror("poll failed,");
return -;
} for (int i = ; i < EVENT_SIZE; i++)
{
if (fds[i].fd == )
{
continue;
} if (fds[i].fd == server && (fds[i].revents & POLLIN))
{ // accept a new client
struct sockaddr_in addrClient;
socklen_t len = sizeof(addrClient);
int client = accept(server, (struct sockaddr*)&addrClient, &len);
if (client == -)
{
perror("accept failed,");
return -;
}
else
{
cout << "accep an new client " << client << ", ip:" << inet_ntoa(addrClient.sin_addr) << endl;
fds[client].fd = client;
fds[client].events = POLLIN;
}
}
else if (fds[i].revents & POLLIN)
{ // read event
memset(recvBuf, , RECV_SIZE + );
if ((ret = recv(fds[i].fd, recvBuf, RECV_SIZE, )) < )
{
perror("recv failed,");
return -;
}
else if (ret == )
{ // client close
close(fds[i].fd);
fds[i].fd = ;
}
else
{ // recv data
cout << "client " << fds[i].fd << " said:" << recvBuf << endl;
memset(sendBuf, , SEND_SIZE + );
sprintf(sendBuf, "You said:%s", recvBuf);
if ((ret = send(fds[i].fd, sendBuf, SEND_SIZE, )) < )
{
perror("send failed,");
return -;
}
}
}
else
{ // write event }
}
}
close(server); return ;
}

Makefile:

echo: poll.cpp
g++ -g -o $@ $< clean:
rm -rf echo

运行结果:

服务器端:

linux poll 学习

客户端1:

linux poll 学习

客户端2:

linux poll 学习

三、参考

http://www.360doc.com/content/12/0821/10/7775902_231465100.shtml

http://blog.chinaunix.net/uid-23842323-id-2656589.html

上一篇:2.10 用最少次数寻找数组中的最大值和最小值[find min max of array]


下一篇:Activity生命周期(一) 暨 帮助文档的使用