網絡編程——C++實現socket通信(TCP)高并發之epoll模式_tcp通信c++ 多客戶端epoll_n大橘為重n的博客-CSDN博客
網絡編程——C++實現socket通信(TCP)高并發之select模式_n大橘為重n的博客-CSDN博客
server.cpp?
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <ctype.h>
#include <sys/epoll.h> //epoll頭文件#define MAXSIZE 1024
#define IP_ADDR "127.0.0.1"
#define IP_PORT 8888int main()
{int i_listenfd, i_connfd;struct sockaddr_in st_sersock;char msg[MAXSIZE];int nrecvSize = 0;struct epoll_event ev, events[MAXSIZE];int epfd, nCounts; //epfd:epoll實例句柄, nCounts:epoll_wait返回值if((i_listenfd = socket(AF_INET, SOCK_STREAM, 0) ) < 0) //建立socket套接字{printf("socket Error: %s (errno: %d)\n", strerror(errno), errno);exit(0);}memset(&st_sersock, 0, sizeof(st_sersock));st_sersock.sin_family = AF_INET; //IPv4協議st_sersock.sin_addr.s_addr = htonl(INADDR_ANY); //INADDR_ANY轉換過來就是0.0.0.0,泛指本機的意思,也就是表示本機的所有IP,因為有些機子不止一塊網卡,多網卡的情況下,這個就表示所有網卡ip地址的意思。st_sersock.sin_port = htons(IP_PORT);if(bind(i_listenfd,(struct sockaddr*)&st_sersock, sizeof(st_sersock)) < 0) //將套接字綁定IP和端口用于監聽{printf("bind Error: %s (errno: %d)\n", strerror(errno), errno);exit(0);}if(listen(i_listenfd, 20) < 0) //設定可同時排隊的客戶端最大連接個數{printf("listen Error: %s (errno: %d)\n", strerror(errno), errno);exit(0);}if((epfd = epoll_create(MAXSIZE)) < 0) //創建epoll實例{printf("epoll_create Error: %s (errno: %d)\n", strerror(errno), errno);exit(-1);}ev.events = EPOLLIN;ev.data.fd = i_listenfd;if(epoll_ctl(epfd, EPOLL_CTL_ADD, i_listenfd, &ev) < 0){printf("epoll_ctl Error: %s (errno: %d)\n", strerror(errno), errno);exit(-1);}printf("======waiting for client's request======\n");//準備接受客戶端連接while(1){if((nCounts = epoll_wait(epfd, events, MAXSIZE, -1)) < 0){printf("epoll_ctl Error: %s (errno: %d)\n", strerror(errno), errno);exit(-1);}else if(nCounts == 0){printf("time out, No data!\n");}else{for(int i = 0; i < nCounts; i++){int tmp_epoll_recv_fd = events[i].data.fd;if(tmp_epoll_recv_fd == i_listenfd) //有客戶端連接請求{if((i_connfd = accept(i_listenfd, (struct sockaddr*)NULL, NULL)) < 0) //阻塞等待客戶端連接{printf("accept Error: %s (errno: %d)\n", strerror(errno), errno);// continue;} else{printf("Client[%d], welcome!\n", i_connfd);}ev.events = EPOLLIN;ev.data.fd = i_connfd;if(epoll_ctl(epfd, EPOLL_CTL_ADD, i_connfd, &ev) < 0){printf("epoll_ctl Error: %s (errno: %d)\n", strerror(errno), errno);exit(-1);}}else //若是已連接的客戶端發來數據請求{//接受客戶端發來的消息并作處理(小寫轉大寫)后回寫給客戶端memset(msg, 0 ,sizeof(msg));if((nrecvSize = read(tmp_epoll_recv_fd, msg, MAXSIZE)) < 0){printf("read Error: %s (errno: %d)\n", strerror(errno), errno);continue;}else if( nrecvSize == 0) //read返回0代表對方已close斷開連接。{printf("client has disconnected!\n");epoll_ctl(epfd, EPOLL_CTL_DEL, tmp_epoll_recv_fd, NULL);close(tmp_epoll_recv_fd); //continue;}else{printf("recvMsg:%s", msg);for(int i=0; msg[i] != '\0'; i++){msg[i] = toupper(msg[i]);}if(write(tmp_epoll_recv_fd, msg, strlen(msg)+1) < 0){printf("write Error: %s (errno: %d)\n", strerror(errno), errno);}}}}}}//whileclose(i_listenfd);close(epfd);return 0;
}
?client.cpp
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <signal.h>
#include <arpa/inet.h>#define MAXSIZE 1024
#define IP_ADDR "127.0.0.1"
#define IP_PORT 8888int i_sockfd = -1;void SigCatch(int sigNum) //信號捕捉函數(捕獲Ctrl+C)
{if(i_sockfd != -1){close(i_sockfd);}printf("Bye~! Will Exit...\n");exit(0);
}int main()
{struct sockaddr_in st_clnsock;char msg[1024];int nrecvSize = 0;signal(SIGINT, SigCatch); //注冊信號捕獲函數if((i_sockfd = socket(AF_INET, SOCK_STREAM, 0) ) < 0) //建立套接字{printf("socket Error: %s (errno: %d)\n", strerror(errno), errno);exit(0);}memset(&st_clnsock, 0, sizeof(st_clnsock));st_clnsock.sin_family = AF_INET; //IPv4協議//IP地址轉換(直接可以從物理字節序的點分十進制 轉換成網絡字節序)if(inet_pton(AF_INET, IP_ADDR, &st_clnsock.sin_addr) <= 0){printf("inet_pton Error: %s (errno: %d)\n", strerror(errno), errno);exit(0);}st_clnsock.sin_port = htons(IP_PORT); //端口轉換(物理字節序到網絡字節序)if(connect(i_sockfd, (struct sockaddr*)&st_clnsock, sizeof(st_clnsock)) < 0) //主動向設置的IP和端口號的服務端發出連接{printf("connect Error: %s (errno: %d)\n", strerror(errno), errno);exit(0);}printf("======connect to server, sent data======\n");while(1) //循環輸入,向服務端發送數據并接受服務端返回的數據{fgets(msg, MAXSIZE, stdin);printf("will send: %s", msg);if(write(i_sockfd, msg, MAXSIZE) < 0) //發送數據{printf("write Error: %s (errno: %d)\n", strerror(errno), errno);exit(0);}memset(msg, 0, sizeof(msg));if((nrecvSize = read(i_sockfd, msg, MAXSIZE)) < 0) //接受數據{printf("read Error: %s (errno: %d)\n", strerror(errno), errno);}else if(nrecvSize == 0){printf("Service Close!\n");}else{printf("Server return: %s\n", msg);}}return 0;
}
?