这篇博客介绍使用poll函数实现并发服务器。
下面贴代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int startup(const char*ip, int port)
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
perror("socket");
exit(-1);
}
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(port);
local.sin_addr.s_addr = inet_addr(ip);
if(bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0)
{
perror("bind");
exit(-1);
}
if(listen(sock, 10) < 0)
{
perror("listen");
exit(-1);
}
return sock;
}
int main()
{
int listen_sock = startup("127.0.0.1",8080);
int maxfd = 0;
struct pollfd evs[5];
evs[0].fd = listen_sock;//需要监测的文件描述符
evs[0].events = POLLIN;
evs[0].revents = 0;
int timeout = 8000;
int nums = sizeof(evs)/sizeof(evs[0]);
struct sockaddr_in client;
socklen_t len = sizeof(client);
int i=1;
for(; i maxfd&&j!=maxfd)//更新有效文件描述符个数
maxfd = j;
}
else if((evs[i].fd>0) && (evs[i].revents)&POLLIN)
{//其他fd就绪,如:read, write...
char buf[1024];
ssize_t s = read(evs[i].fd, buf, sizeof(buf)-1);
if(s > 0)
{//读成功
buf[s] = 0;
printf("client say# %s", buf);
}
//从标准输入中获取字符,写给客户端
ssize_t _s = read(0, buf, sizeof(buf)-1);
if(_s > 0)
{
write(evs[i].fd, buf, strlen(buf));
}
else if(s == 0)
{
printf("read done..., break\n");
//关闭该文件描述符,获取其他有效的文件描述符
close(evs[i].fd);
/*int k = 1;
for(; k < nums; k++)
{
if(evs[k].fd != -1 && (k!=i))
{
int tmp = evs[i].fd;
evs[i].fd = evs[k].fd;
evs[k].fd = tmp;
}
}*/
}
else
{
perror("read");
continue;
}
}
}
}
break;
}
}
return 0;
}
poll函数解析:
#include
int poll(struct pollfd fdarray[], nfds_t nfds, int timeout);
pollfd结构如下:
struct pollfd{
int fd; /* file descriptor to check, or < 0 to ignore */
short events; /* events of interest on fd*/
short revents; /* events that occurred on fd*/
};
fd是监听的文件描述符,
events设置的值,是我们关心的每个文件描述符会发生的哪些事件
revents的值用作返回时,说明每个文件描述符发生了哪些事件,也就是实际发生的事件
events和revents的常用值有POLLIN,POLLRDNORM,POLLRDBAND等等,具体含义可以百度
nfds用来指定fdarray数组中的元素数目。经过测试,如果监听了两个fd,但是nfds==1的情况下,只有fdarray[0].fd能被监听到
timeout指定的是我们愿意等待多长时间。timeout==-1,永远等待;timeout==0,不等待;timeout>0,等待timeout毫秒。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
perror("socket");
return 2;
}
struct sockaddr_in remote;
remote.sin_family = AF_INET;
remote.sin_port = htons(8080);
remote.sin_addr.s_addr = inet_addr("127.0.0.1");
int ret = connect(sock, (struct sockaddr*)&remote, sizeof(remote));
//请求连接
if(ret < 0)
{
perror("connect");
return 3;
}
char buf[1024];
while(1)
{
memset(buf, '\0', sizeof(buf));
printf("Please Enter # ");
fflush(stdout);
ssize_t s = read(0, buf, sizeof(buf)-1);
if(s > 0)
{
write(sock, buf, strlen(buf));
}
//显示服务器传来的消息
ssize_t _s = read(sock, buf, sizeof(buf)-1);
if(_s > 0)
{
printf("server -> client :%s", buf);
}
}
close(sock);
return 0;
}