Linux下TCP/IP网络编程示例——实现服务器/客户端通信(五):基于socket实现基本的服务器与客户端通信,使用多路复用(poll)实现服务端的多并发功能。
poll()和select()没有本质的区别,都是内核去轮询监控所有文件描述符,只不过select()有最大监控数量限制,poll()没有最大监控数量限制。
客户端代码同(一)中的一样:
服务端 server.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <poll.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define SERVER_PORT 6000
#define MAX_CLIENT_NUM 100
int listen_fd = -1;
/*
** 按Ctrl+C退出server程序时,执行下面操作关闭监听套接字
*/
void signal_handler(int arg)
{
printf("close listen_fd(signal = %d)\n", arg);
close(listen_fd);
exit(0);
}
int main(int argc, const char *argv[])
{
int i = 0;
int new_fd = -1;
int poll_timeout = 1000; //1s
int max_nums = 0;
struct pollfd pofd[MAX_CLIENT_NUM] = {0};
struct sockaddr_in server;
struct sockaddr_in client;
socklen_t saddrlen = sizeof(server);
socklen_t caddrlen = sizeof(client);
signal(SIGINT, signal_handler);
memset(&server, 0, sizeof(server));
memset(&client, 0, sizeof(client));
memset(&pofd, 0, sizeof(pofd));
for (i=0; i<MAX_CLIENT_NUM; i++)
{
pofd[i].fd = -1;
}
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd < 0)
{
printf("socket error!\n");
return -1;
}
server.sin_family = AF_INET;
server.sin_port = htons(SERVER_PORT);
server.sin_addr.s_addr = INADDR_ANY;
if (bind(listen_fd, (struct sockaddr *)&server, saddrlen) < 0)
{
printf("bind error!\n");
return -1;
}
if (listen(listen_fd, 5) < 0)
{
printf("listen error!\n");
return -1;
}
pofd[0].fd = listen_fd;
pofd[0].events = POLLIN;
int ready = 0;
size_t rsize = 0;
char rbuf[200] = {0};
while (1)
{
ready = poll(pofd, max_nums+1, poll_timeout);
if (ready < 0)
{
perror("poll");
return -1;
}
else if (ready == 0)
{
continue;
}
if (pofd[0].revents & POLLIN)
{
new_fd = accept(listen_fd, (struct sockaddr *)&client, &caddrlen);
if (new_fd < 0)
{
perror("accept error");
return -1;
}
printf("new client connected(%d).IP:%s,port:%u\n", new_fd, inet_ntoa(client.sin_addr), ntohs(client.sin_port));
for (i=1; i<MAX_CLIENT_NUM; i++)
{
if (pofd[i].fd == -1)
{
pofd[i].fd = new_fd;
pofd[i].events = POLLIN;
break;
}
}
if (i >= MAX_CLIENT_NUM)
{
printf("reached the maximum number of connections!\n");
close(new_fd);
continue;
}
max_nums = (max_nums > i) ? max_nums : i;
}
for (i=1; i<MAX_CLIENT_NUM; i++)
{
if((pofd[i].fd != -1) && (pofd[i].revents & POLLIN))
{
rsize = read(pofd[i].fd, rbuf, sizeof(rbuf));
if (rsize < 0)
{
printf("read error!\n");
continue;
}
else if (rsize == 0)
{
printf("client (fd = %d) is closed!\n", pofd[i].fd);
close(pofd[i].fd);
pofd[i].fd = -1;
continue;
}
else
{
printf("recv from link=%d:%s\n", pofd[i].fd, rbuf);
}
}
}
}
close(listen_fd);
return 0;
}