思维导图
select实现TCP并发服务器
#include <myhead.h>
#define SER_IP "192.168.125.26"
#define SER_PORT 8888int main(int argc, const char *argv[])
{int sfd = socket(AF_INET,SOCK_STREAM,0);if(sfd == -1){perror("socket error");return -1;}printf("socket success sfd = %d\n",sfd);int reuse = 1;if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1){perror("setsockopt error");return -1;}printf("端口号快速重用成功\n");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 error");return -1;}printf("bind success\n");if(listen(sfd,128) == -1){perror("listen error");return -1;}printf("listen success\n");struct sockaddr_in cin;socklen_t socklen = sizeof(cin);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(sfd+1,&tempfds,NULL,NULL,NULL);if(res == 0){printf("time out\n");return -1;}else if(res ==-1){perror("select error");return -1;}for(int i =0;i <= maxfd;i++){if(!FD_ISSET(i,&tempfds)){continue;}if(i == sfd){newfd = accept(sfd,(struct sockaddr*)&cin,&socklen);if(newfd == -1){perror("accept error");return -1;}printf("[%s:%d]:已连接,newfd = %d\n",\inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);FD_SET(newfd,&readfds);cin_arr[newfd] = cin;if(maxfd<newfd){maxfd = newfd;}}else if(i == 0){char buf[128] = "";fgets(buf, sizeof(buf), stdin);buf[strlen(buf)-1] = 0;printf("触发了键盘输入事件:%s\n", buf);}else{char rbuf[128] = ""; //用于接受消息bzero(rbuf, sizeof(rbuf)); //清空容器//从套接字文件中读取消息//int ret = read(newfd, rbuf, sizeof(rbuf));int ret = recv(newfd, rbuf, sizeof(rbuf), 0);if(ret == 0){printf("客户端已下线\n");close(1);FD_CLR(i,&readfds);for(int j=maxfd;j>sfd;j--){if(FD_ISSET(j,&readfds)){maxfd = j;break;}}continue;}printf("[%s:%d]: %s\n", \inet_ntoa(cin.sin_addr), ntohs(cin.sin_port),rbuf );for(int i=4;i<=maxfd;i++){send(i,rbuf,strlen(rbuf),0);}}}}close(sfd);return 0;
}
poll完成TCP的客户端
#define SER_IP "192.168.125.12"
#define SER_PORT 8889
#define CLI_IP "192.168.125.12"
#define CLI_PORT 9999
int main(int argc, const char *argv[])
{int cfd = socket(AF_INET,SOCK_STREAM,0);if(cfd == -1){perror("socket error");return -1;}printf("socket success cfd = %d\n",cfd);int reuse = 1;if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1){perror("setsockopt error");return -1;}printf("端口号快速重用成功\n");struct sockaddr_in cin;cin.sin_family = AF_INET;cin.sin_port = htons(CLI_PORT);cin.sin_addr.s_addr = inet_addr(CLI_IP);if(bimd(cfd,(struct sockaddr*)&cin,sizeof(cin)) == -1){perror("bind error");return -1;}printf("bind success\n");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(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){perror("connect error");return -1;}printf("connect success\n");struct pollfd pfds[2];pfds[0].fd = cfd;pfds[0].events = POLLIN;pfds[1].fd = 0;pfds[1].events = POLLIN;char wbuf[128] = "";while(1){int res = poll(pfds,2,-1);if(res == 0){printf("time out\n");return -1;}else if(res == -1){perror("poll error");return -1;}if(pfds[0].revents == POLLIN){fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1] = '\0';if(strcmp(wbuf,"quit") == 0){break;}send(cfd,wbuf,strlen(wbuf),0);}if(pfds[1].revents == POLLIN){bzero(wbuf,sizeof(wbuf));recv(cfd,wbuf,sizeof(wbuf),0);printf("收到服务器的信息为:%s\n",wbuf);}}close(cfd);return 0;
}