功能:阻塞函数,阻塞等待要监听的集合中的文件描述符准备就绪,如果有文件描述符准备就绪,则当前函数立即解除阻塞.
原型:
#include <poll.h>
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
参数:
struct pollfd *fds:要监控的文件描述符集合;与select的区别,select是以事件问单位,poll是以文件描述符为单位。
struct pollfd {
int fd; /* file descriptor */ 要检测文件描述符
short events; /* requested events */ 要检测的事件
short revents; /* returned events */ 实际产生的事件
};
事件:
POLLIN 读事件,有数据可读。
POLLOUT 写事件,有数据可写
POLLERR 错误事件,只会出现在实际产生的时间中。
nfds_t nfds:指定要监控的文件描述符个数;
int timeout:
>0, 超时时间;以ms为单位; 例如3s == 3000ms。
<0, 如果不想设置超时时间,则填负数。
=0,则该函数不阻塞,即使没有文件描述符准备就绪;
返回值:
>0, 成功触发事件的文件描述符个数;
=0, 超时,且没有文件描述符准备就绪
=-1, 函数运行失败,更新errno;
使用poll函数 实现简单服务器和客户端
服务端支持并发连接,同时支持终端输入信息发送给客户端
客户端支持显示接收到的服务端消息, 同时支持终端输入消息发送服务器
server.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <stdlib.h>
#include <poll.h>
#define PRINT_ERROR(msg) do{\
fprintf(stderr, "line:__%d__ ", __LINE__);\
perror(msg);\
exit(-1);\
}while(0)
#define CONN_COUNT 10
int findIndex(struct pollfd* fds) {
for (int i = 0; i < CONN_COUNT; i++) {
if (*((int *)(fds + i)) == 0) {
return i;
}
}
return -1;
}
int main(int argc, const char *argv[]) {
int sfd;
if((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
PRINT_ERROR("socket");
}
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
PRINT_ERROR("setsockopt");
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(atoi(argv[2]));
sin.sin_addr.s_addr = inet_addr(argv[1]);
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
PRINT_ERROR("bind");
}
if(listen(sfd, 10) < 0) {
PRINT_ERROR("listen");
}
int accept_fd = 0;
int nbytes = 0;
char buf[128] = {0};
int p_res = 0;
struct pollfd fds[CONN_COUNT] = {0};
fds[0].fd = 0;
fds[0].events = POLLIN;
fds[1].fd = sfd;
fds[1].events = POLLIN;
int next = 2;
while (1) {
if ((p_res = poll(fds, next, 10000)) == -1) {
PRINT_ERROR("select");
} else if (p_res == 0) {
printf("time out...\n");
}
if (fds[0].revents & POLLIN) {
memset(buf, 0, sizeof(buf));
int fd = 0;
int res = scanf("%d %s", &fd, buf);
while (getchar() != 10);
if (res != 2 || fd > next) {
printf("please input correct data\n");
continue;
}
if (send(fd, buf, sizeof(buf), 0) == -1) {
PRINT_ERROR("send");
}
p_res --;
}
if (fds[1].revents & POLLIN) {
if ((accept_fd = accept(sfd, NULL, NULL)) == -1) {
PRINT_ERROR("accept error");
}
printf("client [%d] connect!!!\n", accept_fd);
fds[next].fd = accept_fd;
fds[next].events = POLLIN;
next ++;
p_res --;
}
for (int i = 2; i < next && p_res != 0; i++) {
if (fds[i].revents & POLLIN) {
memset(buf, 0, sizeof(buf));
if ((nbytes = recv(fds[i].fd, buf, sizeof(buf), 0)) == -1) {
PRINT_ERROR("recv error");
} else if (nbytes == 0) {
printf("client [%d] disconnect !!!\n", i);
close(fds[i].fd);
for (int j = i; j < next; j++) {
fds[j] = fds[j + 1];
}
next --;
continue;
}
if (!strncmp(buf, "quit", 4)) {
printf("client [%d] disconnect !!!\n", fds[i].fd);
close(fds[i].fd);
for (int j = i; j < next; j++) {
fds[j] = fds[j + 1];
}
next --;
continue;
}
strcat(buf, "---hi !!!");
if (send(fds[i].fd, buf, sizeof(buf), 0) == -1) {
PRINT_ERROR("send");
}
p_res --;
}
}
}
close(sfd);
return 0;
}
client.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <stdlib.h>
#include <poll.h>
#define PRINT_ERROR(msg) do{\
fprintf(stderr, "line:__%d__ ", __LINE__);\
perror(msg);\
exit(-1);\
}while(0)
int main(int argc, const char *argv[]) {
int sfd;
if((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
PRINT_ERROR("socket");
}
struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[2]));
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
socklen_t serveraddr_len = sizeof(serveraddr);
if (connect(sfd, (struct sockaddr *)&serveraddr, serveraddr_len) == -1) {
PRINT_ERROR("connect error");
}
int accept_fd = 0;
int nbytes = 0;
char buf[128] = {0};
int p_res = 0;
struct pollfd fds[2] = {0};
fds[0].fd = 0;
fds[0].events = POLLIN;
fds[1].fd = sfd;
fds[1].events = POLLIN;
while (1) {
if ((p_res = poll(fds, 2, 10000)) == -1) {
PRINT_ERROR("select");
} else if (p_res == 0) {
printf("time out...\n");
}
if (fds[0].revents & POLLIN) {
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
if (send(sfd, buf, sizeof(buf), 0) == -1) {
PRINT_ERROR("send");
}
if (!strncmp(buf, "quit", 4)) {
close(sfd);
return 0;
}
}
if (fds[1].revents & POLLIN) {
memset(buf, 0, sizeof(buf));
if ((nbytes = recv(sfd, buf, sizeof(buf), 0)) == -1) {
PRINT_ERROR("recv error");
} else if (nbytes == 0) {
printf("server quit !!!!");
break;
}
printf("receive ----> %s\n", buf);
}
}
close(sfd);
return 0;
}
执行结果
server
ubuntu@ubuntu:~ $ ./server 127.0.0.1 8889
client [4] connect!!!
client [5] connect!!!
4 hi
5 hello
client [5] disconnect !!!
client [4] disconnect !!!
^C
ubuntu@ubuntu:~ $
client1
ubuntu@ubuntu:~ $ ./client 127.0.0.1 8889
1
receive ----> 1---hi !!!
2
receive ----> 2---hi !!!
3
receive ----> 3---hi !!!
receive ----> hi
quit
ubuntu@ubuntu:~ $
client2
ubuntu@ubuntu:~ $ ./client 127.0.0.1 8889
a
receive ----> a---hi !!!
b
receive ----> b---hi !!!
c
receive ----> c---hi !!!
receive ----> hello
quit
ubuntu@ubuntu:~ $