selectIO多路复用并发服务器,是通过轮询检测文件描述符来实现并发
将内核要检测文件描述符放入集合中,调用select函数,通知内核区检测文件描述符集合中的文件描述符是否准备就绪,即对应的空间中是否有数据
对准备就绪的文件描述符指向对应的IO操作,让再循环通过select监测
objectivec
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d ", __LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.114.134" //本机IP ifconfig
#define PORT 8899 //1024-49151
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(PF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("sfd = %d\n", sfd);
//允许端口快速重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用成功\n");
//填充地址信息结构体,
//真实的地址信息结构体根据地址族指定 AF_INET:man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET;
sin.sin_port = htons(PORT) ; //端口号,1024~49151
sin.sin_addr.s_addr = inet_addr(IP); //本机IP, ifconfig
//将IP和端口绑定到套接字上;
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n", __LINE__);
//将套接字设置为被动监听状态,监听是否有客户端连接成功;
if(listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n", __LINE__);
//创建select队列结构体
fd_set readfds , tempfds;
FD_ZERO(&readfds);
FD_SET(sfd,&readfds);
FD_SET(0,&readfds);
int maxfd = sfd;
int sel = 0;
struct sockaddr_in cin; //存储连接成功的客户端地址信息
socklen_t addrlen = sizeof(cin);
char buf[1024] = "";
ssize_t res = 0; //读取到的个数
//用来存储对应用户的地址信息结构体
struct sockaddr_in client_arr[1020];
//循环监听文件描述符是否启动
while (1)
{
tempfds = readfds;
sel = select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(sel<0)
{
ERR_MSG("select");
return -1;
}
else if (sel == 0)
{
printf("超时\n");
return -1;
}
for (int i = 0;i<=maxfd;i++)
{
if (FD_ISSET(i,&tempfds))
{
if (i == 0)
{
int send_fd = -1; //要发给那个用户对应的文件描述符
int res_sc = 0;
printf("终端有数据写入\n");
//不将缓存区中的内容取出
//select会一直监听到该文件描述准备就绪
res_sc = scanf("%d %s",&send_fd,buf);
if (res_sc < 2)
{
fprintf(stderr,"请输入正确的格式(int string )\n");
continue;
}
if (send_fd < sfd || send_fd > 1024 || FD_ISSET(send_fd,&readfds))
{
fprintf(stderr,"文件描述符错误\n");
continue;
}
if(send(send_fd,buf,sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
}
else if (sfd == i)
{
//阻塞函数,从已完成连接的队列头中获取一个客户端信息,生成一个新的文件描述符,
//该文件描述符才是与客户端通信的文件描述符!!!
int newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d] newfd=%d 连接成功__%d__\n", \
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);
client_arr[newfd-4] = cin;
FD_SET(newfd,&readfds);
maxfd = maxfd>newfd?maxfd:newfd;
}
else
{
bzero(buf, sizeof(buf));
//接收
res = recv(i, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("[%s:%d] newfd=%d 客户端下线__%d__\n", \
inet_ntoa(client_arr[i-4].sin_addr),
ntohs(client_arr[i-4].sin_port), i, __LINE__);
close(i);
FD_CLR(i,&readfds);
maxfd = maxfd==i?maxfd-1:maxfd;
continue;
}
printf("[%s:%d] newfd=%d : %s __%d__\n", \
inet_ntoa(client_arr[i-4].sin_addr),
ntohs(client_arr[i-4].sin_port), i, buf, __LINE__);
strcpy(buf,"收到");
if(send(i, buf, strlen(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
}
}
}
/*
while(1)
{
bzero(buf, sizeof(buf));
bzero(send_b, sizeof(send_b));
//接收
res = recv(newfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("[%s:%d] newfd=%d 客户端下线__%d__\n", \
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);
break;
}
printf("[%s:%d] newfd=%d : %s __%d__\n", \
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf, __LINE__);
//发送 将数据拼接字符串后发送回去
fprintf(stdout,"回答的内容(不要超过1024字节)>>");
fscanf(stdin,"%s",send_b);
if(send(newfd, send_b, sizeof(send_b), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
*/
close(sfd);
return 0;
}