// mul_port_client_epoll.c// 源码链接: #include #include #include #include #include #include #include #include #include #include #include #include #include #include#define MAX_BUFFER128#define MAX_EPOLLSIZE (384*1024)#define MAX_PORT1 #define TIME_SUB_MS(tv1, tv2)((tv1.tv_sec - tv2.tv_sec) * 1000 + (tv1.tv_usec - tv2.tv_usec) / 1000) int isContinue = 0; static int ntySetNonblock(int fd) { int flags;flags = fcntl(fd, F_GETFL, 0); if (flags < 0) return flags; flags |= O_NONBLOCK; if (fcntl(fd, F_SETFL, flags) < 0) return -1; return 0;} static int ntySetReUseAddr(int fd) { int reuse = 1; return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)}int main(int argc, char **argv) { if (argc <= 2) { printf("Usage: %s ip port\n", argv[0]); exit(0); }// 获取要连接的服务端的ip和端口 const char *ip = argv[1]; int port = atoi(argv[2]);int connections = 0; char buffer[128] = {0}; int i = 0, index = 0;// 创建epoll struct epoll_event events[MAX_EPOLLSIZE]; int epoll_fd = epoll_create(MAX_EPOLLSIZE);strcpy(buffer, " Data From MulClient\n");// 初始化服务器地址 struct sockaddr_in addr; memset( addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr(ip);struct timeval tv_begin; gettimeofday(while (1){ if (++index >= MAX_PORT) index = 0;struct epoll_event ev; int sockfd = 0;// 如果连接数小于340000 , 继续连接服务器 if (connections < 340000if (sockfd == -1) {perror("socket"); goto err;}//ntySetReUseAddr(sockfd);addr.sin_port = htons(port + index);// 连接服务器 if (connect(sockfd, (struct sockaddr*) goto err;}ntySetNonblock(sockfd);// 将套接字设置为非阻塞ntySetReUseAddr(sockfd); // 设置可重用本地地址// 向服务器发送数据 sprintf(buffer, "Hello Server: client --> %d\n", connections);send(sockfd, buffer, strlen(buffer), 0);// 将套接字设置为可读可写 , 然后加入到epoll_wait()中ev.data.fd = sockfd;ev.events = EPOLLIN | EPOLLOUT;epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sockfd,connections ++;}// 如果每连接了一千个客户端或者连接数超过340000 , 那么就执行这个条件 if (connections % 1000 == 999 || connections >= 340000){ struct timeval tv_cur; memcpy(gettimeofday(// 打印一下每连接1000个客户端所消耗的时间 int time_used = TIME_SUB_MS(tv_begin, tv_cur); printf("connections: %d, sockfd:%d, time_used:%d\n", connections, sockfd, time_used);// 进行epoll_wait() int nfds = epoll_wait(epoll_fd, events, connections, 100); for (i = 0;i < nfds;i ++){ int clientfd = events[i].data.fd;// 执行写 if (events[i].eventssend(sockfd, buffer, strlen(buffer), 0);}// 执行读 else if (events[i].eventsssize_t length = recv(sockfd, rBuffer, MAX_BUFFER, 0); if (length > 0) { printf(" RecvBuffer:%s\n", rBuffer);if (!strcmp(rBuffer, "quit")) {isContinue = 0;}} else if (length == 0) { printf(" Disconnect clientfd:%d\n", clientfd);connections --;close(clientfd);} else { if (errno == EINTR) continue;printf(" Error clientfd:%d, errno:%d\n", clientfd, errno);close(clientfd);}} else { printf(" clientfd:%d, errno:%d\n", clientfd, errno);close(clientfd);}}}// 休眠1000微秒(0.01秒)usleep(1 * 1000); } return 0;err: printf("error : %s\n", strerror(errno)); return 0;}
推荐阅读
-
十分父母|网友评论打得好,这种教育方式你怎么看,包贝尔当众怒扇女儿耳光
-
萱草|限量!Alibaba首发“Java成长笔记”,差距不止一点点
-
-
车宇无际|都比较经济实用,十来万就能拿下,这3款高品质SUV
-
-
-
中国新闻网客户端|一线战“疫”日记:防护服上的“生日祝福”
-
中国新闻网|山西吕梁:消防员热传“90岁老兵拄拐重温入党誓词”照片
-
-
历史的长河故事|纪传体三国评传:《晋书笔记·帝纪第一》全十七篇
-
-
汪小菲|汪小菲妈妈自曝确诊新冠,带病坚持直播心态豁达,感染途径曝光
-
-
凇清幕雨 泡泡袖显小女人,气质顿时温柔了,当张雨绮穿上淡黄色裙子
-
Blues蓝调时尚|细腰长腿的个性穿搭,最符合夏天,超适合亚洲人的欧美性感风
-
-
-
-
家庭|【文明榜样】喜讯!云浮2户家庭获评2020年广东省五好家庭
-
新华网|管好用好人民群众的“救命钱”——解读医保基金监管制度体系改革的指导意见