用select实现服务器并发
ser
#include <myhead.h> #define ERR_MSG(msg) do{\fprintf(stderr, "__%d__", __LINE__);\perror(msg);\
}while(0)#define PORT 8888 //端口号,范围1024~49151
#define IP "192.168.1.205" //本机IP,ifconfig int main(int argc, const char *argv[])
{//创建流式套接字 socketint sfd = socket(AF_INET, SOCK_STREAM, 0);if(sfd < 0){ERR_MSG("socket");return -1;}printf("socket create success 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");//填充地址信息结构体给bind函数绑定//真实的地址信息结构体根据地址族制定AF_INET:man 7 ipstruct sockaddr_in sin;sin.sin_family = AF_INET; //必须填AF_INETsin.sin_port = htons(PORT); //端口号的网络字节序sin.sin_addr.s_addr = inet_addr(IP); //本机IP//绑定服务器的地址信息 -->必须绑定 bindif(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0){ERR_MSG("bind");return -1;}printf("bind success\n");//将套接字设置为被动监听状态 listenif(listen(sfd, 128) < 0){ERR_MSG("listen");return -1;}printf("listen success\n");//创建一个读集合,一个副本备份fd_set readfds, tempfds;//清空集合FD_ZERO(&readfds);//将需要检测的文件描述符添加到集合中FD_SET(0, &readfds);FD_SET(sfd, &readfds);int maxfd = sfd; //存储最大的文件描述符int s_res = -1;ssize_t res = -1;char buf[128] = "";int newfd = -1;struct sockaddr_in cin; //存储客户端的地址信息socklen_t addrlen = sizeof(cin); //真实的地址信息结构体大小while(1){tempfds = readfds;//执行IO多路复用函数s_res = select(maxfd+1, &tempfds, NULL, NULL, NULL);if(s_res < 0){ERR_MSG("select");return -1;}else if(0 == s_res){printf("time out...\n");break;}printf("__%d__", __LINE__);//判断是否是0号if(FD_ISSET(0, &tempfds)){printf("触发键盘输入事件\n");bzero(buf, sizeof(buf));fgets(buf, sizeof(buf), stdin);buf[strlen(buf)-1] = 0;printf(":%s\n", buf);}//判断是否是sfdif(FD_ISSET(sfd, &tempfds)){printf("触发客户端连接事件\n");newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);if(newfd < 0){ERR_MSG("newfd");return -1;}printf("[%s:%d]客户端连接成功 newfd=%d\n",\inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);FD_SET(newfd, &readfds); //将newfd添加到集合中maxfd = maxfd>newfd ? maxfd:newfd; //更新maxfd}//除了0和sfd事件外,其余均为newfd对应的客户端交互事件//通过循环i(i是文件描述符),遍历所有newfd是否在tempfds中for(int i=4; i<=maxfd; i++){if(FD_ISSET(i, &tempfds)){printf("触发客户端交互事件\n");//清空字符串bzero(buf, sizeof(buf)); //也可以用memset//接收res = recv(i, buf, sizeof(buf), 0);if(res < 0){ERR_MSG("recv");return -1;}else if(0 == res){printf("[%s:%d]客户端下线 newfd=%d\n",\inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), i);//关闭文件描述符close(i);//将文件描述符从集合中剔除FD_CLR(i, &readfds);//由于剔除的文件描述符可能是最大的文件描述符,所以要更新maxfdwhile(FD_ISSET(maxfd, &readfds)==0 && maxfd-->=0);}printf("[%s:%d] newfd=%d : %s\n", \inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), i, buf);//发送strcat(buf, "O.o");if(send(i, buf, sizeof(buf), 0) < 0){ERR_MSG("send");return -1;}printf("send success\n");}}}//关闭文件描述符if(close(sfd) < 0){ERR_MSG("close");return -1;}return 0;
}
cli
#include <myhead.h> #define ERR_MSG(msg) do{\fprintf(stderr, "__%d__", __LINE__);\perror(msg);\
}while(0)#define PORT 8888 //端口号,范围1024~49151
#define IP "192.168.1.205" //本机IP,ifconfig int main(int argc, const char *argv[])
{//创建流式套接字 socketint cfd = socket(AF_INET, SOCK_STREAM, 0);if(cfd < 0){ERR_MSG("socket");return -1;}printf("socket create success cfd=%d\n", cfd);//填充地址信息结构体给bind函数绑定//真实的地址信息结构体根据地址族制定AF_INET:man 7 ipstruct sockaddr_in sin;sin.sin_family = AF_INET; //必须填AF_INETsin.sin_port = htons(PORT); //端口号的网络字节序sin.sin_addr.s_addr = inet_addr(IP); //本机IP//连接指定服务器 connectif(connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) < 0){ERR_MSG("connect");return -1;}printf("connect success\n");//定义要检测的集合struct pollfd fds[2];memset(fds, 0, sizeof(fds));//将需要的文件描述符添加到集合中fds[0].fd = 0; //指定监测0号文件描述符fds[0].events = POLLIN; //指定监测读事件fds[1].fd = cfd;fds[1].events = POLLIN;int nfds = 2;int p_res = -1;char buf[128] = "";ssize_t res = 0;while(1){p_res = poll(fds, nfds, -1);if(p_res < 0){ERR_MSG("poll");return -1;}else if(0 == p_res){printf("time out...\n");break;}//能运行到当前位置,则代表集合中有文件描述符准备就绪了 通过按位与的方式提取revents中代表POLLIN的哪一个bitif((fds[0].revents & POLLIN) != 0){printf("触发键盘输入事件\n");//清空字符串bzero(buf, sizeof(buf)); //也可以用memsetprintf("请输入>>> ");fgets(buf ,sizeof(buf), stdin);buf[strlen(buf)-1] = 0;//发送if(send(cfd, buf, sizeof(buf), 0) < 0){ERR_MSG("send");return -1;}printf("send success\n");}if(fds[1].revents & POLLIN){printf("触发服务器交互事件\n");bzero(buf, sizeof(buf)); //也可以用memset//接收res = recv(cfd, buf, sizeof(buf), 0);if(res < 0){ERR_MSG("recv");return -1;}else if(0 == res){printf("服务器下线 cfd=%d\n", cfd);break;}printf("cfd=%d : %s\n", cfd, buf);}}//关闭文件描述符if(close(cfd) < 0){ERR_MSG("close");return -1;}return 0;
}
现象