这篇博客介绍使用poll函数实现并发服务器。
下面贴代码
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <poll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
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<nums; i++)
{
evs[i].fd = -1;
}
while(1)
{
switch(poll(evs, maxfd+1, timeout))
{
case 0:
printf("timeout...\n");
break;
case -1:
perror("poll");
break;
default:
{
for(i=0; i<nums; i++)
{//listen_sock的读事件就绪,获取客户端fd
if((evs[i].fd == listen_sock) && (evs[i].revents)&POLLIN)
{
//获取客户端
int new_sock = accept(listen_sock, (struct sockaddr*)&client, &len);
if(new_sock < 0)
{//取数据失败
perror("accept");
continue;
}
printf("get new client:[%s:%d]\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
//将获取的客户端new_sock放入poll结构体数组中,以便poll()监测
int j=1;
for(; j<nums; j++)
{
if(evs[j].fd == -1)
{//将new_sock添加进去
evs[j].fd = new_sock;
evs[j].events = POLLIN;
evs[j].revents = 0;
break;
}
}
if(j == nums)
{
close(new_sock);
}
if(j > 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 <poll.h>
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 <stdio.h>
#include <errno.h>
#include <poll.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdlib.h>
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;
}