select实现的TCP并发服务器
#include <myhead.h>
#define SER_PORT 8888
#define SER_IP "192.168.47.130"int main(int argc, const char *argv[])
{int sfd = -1;sfd = socket(AF_INET,SOCK_STREAM,0);if(sfd == -1){perror("socket");return -1;}printf("sfd = %d\n",sfd);int reuse = 1;if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1){perror("setsockopt");return -1;}struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(SER_PORT);sin.sin_addr.s_addr = inet_addr(SER_IP);if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){perror("bind");return -1;}printf("bind success\n");if(listen(sfd,128) == -1){perror("listen");return -1;}printf("listen success\n");int newfd = -1;struct sockaddr_in cin;socklen_t addrlen = sizeof(cin);char sbuf[128] = {0};fd_set readfds,tempfds;FD_ZERO(&readfds);FD_SET(0,&readfds);FD_SET(sfd,&readfds);int maxfd = sfd;struct sockaddr_in cin_arr[1024];while(1){tempfds = readfds;int res = select(maxfd+1,&tempfds,NULL,NULL,NULL);if(res == -1){perror("select");return -1;}else if(res == 0){printf("time out\n");return -1;}for(int i = 0;i<=maxfd;i++){if(!FD_ISSET(i,&tempfds)){continue;}if(i == sfd){if((newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen)) == -1){perror("accept");return -1;}printf("[%s %d]:发来连接请求\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));cin_arr[newfd] = cin;FD_SET(newfd,&readfds);if(newfd > maxfd){maxfd = newfd;}}else if(i == 0){fgets(sbuf,sizeof(sbuf),stdin);sbuf[strlen(sbuf)-1] = 0;printf("触发了键盘输入事件:%s\n",sbuf);for(int i = 4;i<maxfd;i++){send(i,sbuf,sizeof(sbuf),0);}printf("发送成功\n");}else{char rbuf[128] = {0};bzero(rbuf,sizeof(rbuf));int res = recv(i,rbuf,sizeof(rbuf)-1,0);if(res == 0){printf("客户端已经下线\n");close(i);FD_CLR(i,&readfds);for(int k = maxfd;k >= sfd;k--){if(FD_ISSET(k,&readfds)){maxfd = k;break;}}continue;}printf("[%s %d]:%s\n",inet_ntoa(cin_arr[i].sin_addr),ntohs(cin_arr[i].sin_port),rbuf);strcat(rbuf,">_<");send(i,rbuf,strlen(rbuf),0);printf("发送成功\n");}}}close(sfd);return 0;
}
poll实现的TCP客户端
#include<myhead.h>
#define SER_PORT 8888
#define SER_IP "192.168.117.74"
#define CLI_PORT 9999
#define CLI_IP "192.168.117.74"int main(int argc, const char *argv[])
{//1、创建用于连接的客户端套接字int cfd = socket(AF_INET, SOCK_STREAM, 0);if(cfd == -1){perror("socket error");return -1;}printf("socket success cfd = %d\n", cfd); //3//设置端口号快速重用int reuse = 1;if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) ==-1){perror("setsockopt error");return -1;}printf("端口号快速重用成功\n");//2、绑定端口号和ip地址(非必须)//2.1 填充客户端地址信息结构体struct sockaddr_in cin;cin.sin_family = AF_INET;cin.sin_port = htons(CLI_PORT);cin.sin_addr.s_addr = inet_addr(CLI_IP);//2.2 绑定端口号和IPif(bind(cfd, (struct sockaddr*)&cin, sizeof(cin)) == -1){perror("bind error");return -1;}printf("bind success\n");//3、连接服务器//3.1 填充要连接服务器的地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET; //地址族sin.sin_port = htons(SER_PORT); //服务器端口号sin.sin_addr.s_addr = inet_addr(SER_IP); //服务器的IP地址//3.2 连接服务器if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) == -1){perror("connect error");return -1;}printf("连接成功!\n");//使用poll完成0号文件描述符和cfd文件描述符的多路复用//11、准备文件描述符容器struct pollfd pfds[2];pfds[0].fd = 0; //文件描述符pfds[0].events = POLLIN; //检测读事件pfds[1].fd = cfd; //文件描述符pfds[1].events = POLLIN; //检测读事件//4、收发数据char wbuf[128] = "";while(1){int res = poll(pfds, 2, -1); //阻塞检测集合中是否有事件产生if(res == -1){perror("poll error");return -1;}else if(res == 0){printf("time out\n");return -1;}//程序执行至此,说明检测的文件描述符集合中有事件产生//判断是否为0号文件描述符产生事件if(pfds[0].revents == POLLIN){fgets(wbuf, sizeof(wbuf), stdin); //从终端上获取一个字符串wbuf[strlen(wbuf)-1] = '\0'; //将换行换成 '\0'//判断输入的字符串值if(strcmp(wbuf, "quit") ==0){break;}//将数据发送给服务器send(cfd, wbuf, strlen(wbuf), 0);}//判断释放为cfd文件描述符中产生事件if(pfds[1].revents == POLLIN){//将字符数组清空bzero(wbuf, sizeof(wbuf));recv(cfd, wbuf, sizeof(wbuf)-1, 0);printf("收到服务器消息为:%s\n", wbuf);}}//5、关闭套接字close(cfd);return 0;
}