LINUX 精通 3.2

  1. main里怎么实现

    accept_cb 里的regist部分抽出来

    c 复制代码
    // regist拉出来
    int event_register(int fd, int event) {
    
    	if (fd < 0) return -1;
    
    	conn_list[fd].fd = fd;
    	conn_list[fd].r_action.recv_callback = recv_cb;
    	conn_list[fd].send_callback = send_cb;
    
    	memset(conn_list[fd].rbuffer, 0, BUFFER_LENGTH);
    	conn_list[fd].rlength = 0;
    
    	memset(conn_list[fd].wbuffer, 0, BUFFER_LENGTH);
    	conn_list[fd].wlength = 0;
    
    	set_event(fd, event);
    }
    
    
    
    
    // listenfd(sockfd) --> EPOLLIN --> accept_cb
    int accept_cb(int fd) {
    
    	struct sockaddr_in  clientaddr;
    	socklen_t len = sizeof(clientaddr);
    
    	int clientfd = accept(fd, (struct sockaddr*)&clientaddr, &len);
    	printf("accept finshed: %d\n", clientfd);
    
    /* --------regist--------------
        conn_list[clientfd].fd = clientfd;
        conn_list[clientfd].r_action.recv_callback = recv_cb;
        conn_list[clientfd].send_callback = send_cb;
    
        memset(conn_list[clientfd].rbuffer, 0, BUFFER_LENGTH);
        conn_list[clientfd].rlength = 0;
    
        memset(conn_list[clientfd].wbuffer, 0, BUFFER_LENGTH);
        conn_list[clientfd].wlength = 0;
     
        set_event(clientfd, EPOLLIN);
     --------regist--------------*/
    
    
        event_register(fd, EPOLLIN);
    	return 0;
    }

    main里

    c 复制代码
    int main(){
        unsigned short port = 2000;
        int sockfd = init_server(port);
    
        epfd = epoll_create(1);
        set_event(sockfd, EPOLLIN);
    
    
    	while (1) { // mainloop
    
    		struct epoll_event events[1024] = {0};
    		int nready = epoll_wait(epfd, events, 1024, -1);
    
    		int i = 0;
    		for (i = 0;i < nready;i ++) {
    
    			int connfd = events[i].data.fd;
    #if 0
    //读写二选一
    			if (events[i].events & EPOLLIN) {
    				conn_list[connfd].r_action.recv_callback(connfd);
    			} else if (events[i].events & EPOLLOUT) {
    				conn_list[connfd].send_callback(connfd);
    			}
    
    #else 
    // 读写共存
    			if (events[i].events & EPOLLIN) {
    				conn_list[connfd].r_action.recv_callback(connfd);
    			} 
    
    			if (events[i].events & EPOLLOUT) {
    				conn_list[connfd].send_callback(connfd);
    			}
    #endif
    
            }
        }
    
    
    }

    再改进一下set_event的flag 标志add 还是mod

    c 复制代码
    #include <errno.h>
    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <pthread.h>
    #include <unistd.h>
    #include <poll.h>
    #include <sys/epoll.h>
    #include <errno.h>
    #include <sys/time.h>
    
    
    #define BUFFER_LENGTH 1024
    typedef int (*RCALLBACK)(int fd); //用于定义一个函数指针类型RCALLBACK。它表示一个接受一个int类型参数fd并返回int类型的函数指针
    
    #define CONNECTION_SIZE			1048576 // 1024 * 1024
    
    
    int accept_cb(int fd);
    int recv_cb(int fd);
    int send_cb(int fd);
    
    
    // global variable
    // 抄epoll里构建事件, 把io加入epoll里 不一样封装到set_event里
    // int epfd = epoll_create(1); //不可以这样写,在里面改值
    int epfd = 0;
    
    
    struct conn{
        int fd; //io是fd
    
        char rbuffer[BUFFER_LENGTH];
        int rlength;
        char wbuffer[BUFFER_LENGTH];
        int wlength;
    
        RCALLBACK send_callback;
    
        // epollin事件对应callback要么accept或者recv
        /*
        这段代码定义了一个联合体(union)r_action,它包含两个成员:accept_callback和recv_callback,它们都是RCALLBACK类型的函数指针。
        在使用union时,各成员共享同一块内存空间,只能同时使用其中的一个成员。不同成员可以存储不同类型的数据,但在任意给定时间点上只能使用一个成员。
        该联合体的目的可能是为了提供某种灵活性,在特定情况下可以选择调用accept_callback或recv_callback函数来处理相应的操作。
        */
        union{
            RCALLBACK accept_callback;
            RCALLBACK recv_callback;
        }r_action;
    
    
    };
    
    struct conn conn_list[CONNECTION_SIZE] = {0};
    // fd
    
    
    
    
    int init_server(unsigned short port){
        // 创建抄network里
        // 创建socket
        int sockfd = socket(AF_INET, SOCK_STREAM, 0);
        
        // 绑定本地端口
        struct sockaddr_in servaddr;
        servaddr.sin_family = AF_INET; 
        servaddr.sin_addr.s_addr = htons(INADDR_ANY); //绑网卡地址 默认0.0.0.0 绑本地地址;htons转成网络字节序 
        servaddr.sin_port = htons(port); //改这里 0-1023系统默认 大于1024都能用
    
        if(-1 == bind(sockfd, (struct sockaddr*)&servaddr, sizeof(struct sockaddr))){
            printf("bind failed: %s\n", strerror(errno));
        }
        
        listen(sockfd, 10);
        printf("listen finished: %d\n", sockfd);
    
        return sockfd;
    }
    
    
    int set_event(int fd, int event, int flag){
        
        if(flag){
            struct epoll_event ev; //构建事件,只用来add和delete,control里没用
            ev.events = event;  //改这里不用epoll,reactor用event
            ev.data.fd = fd;
            epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); 
        }
        else{
            struct epoll_event ev; //构建事件,只用来add和delete,control里没用
            ev.events = event;  //改这里不用epoll,reactor用event
            ev.data.fd = fd;
            epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &ev); 
        }
    }
    
    
    // regist拉出来
    int event_register(int fd, int event) {
    
    	if (fd < 0) return -1;
    
    	conn_list[fd].fd = fd;
    	conn_list[fd].r_action.recv_callback = recv_cb;
    	conn_list[fd].send_callback = send_cb;
    
    	memset(conn_list[fd].rbuffer, 0, BUFFER_LENGTH);
    	conn_list[fd].rlength = 0;
    
    	memset(conn_list[fd].wbuffer, 0, BUFFER_LENGTH);
    	conn_list[fd].wlength = 0;
    
    	set_event(fd, event, 1);
    }
    
    
    
    
    // listenfd(sockfd) --> EPOLLIN --> accept_cb
    int accept_cb(int fd) {
    
    	struct sockaddr_in  clientaddr;
    	socklen_t len = sizeof(clientaddr);
    
    	int clientfd = accept(fd, (struct sockaddr*)&clientaddr, &len);
    	printf("accept finshed: %d\n", clientfd);
    
    /* --------regist--------------
        conn_list[clientfd].fd = clientfd;
        conn_list[clientfd].r_action.recv_callback = recv_cb;
        conn_list[clientfd].send_callback = send_cb;
    
        memset(conn_list[clientfd].rbuffer, 0, BUFFER_LENGTH);
        conn_list[clientfd].rlength = 0;
    
        memset(conn_list[clientfd].wbuffer, 0, BUFFER_LENGTH);
        conn_list[clientfd].wlength = 0;
     
        set_event(clientfd, EPOLLIN);
     --------regist--------------*/
    
    
        event_register(fd, EPOLLIN);
    	return 0;
    }
    
    
    int recv_cb(int fd) {
        
        int count = recv(fd, conn_list[fd].rbuffer, BUFFER_LENGTH, 0);
    	if (count == 0) { // disconnect
    		printf("client disconnect: %d\n", fd);
    		close(fd);
    
    		epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); // unfinished
    
    		return 0;
    	} 
        printf("RECV: %s\n", conn_list[fd].rbuffer);
        set_event(fd, EPOLLOUT, 0);
    
        return count;
    }
    
    int send_cb(int fd) {
        
        int count = send(fd, conn_list[fd].wbuffer, count, 0);
    
        set_event(fd, EPOLLIN, 0);
    
        return count;
    }
    
    
    int main(){
        unsigned short port = 2000;
        int sockfd = init_server(port);
    
        epfd = epoll_create(1);
        set_event(sockfd, EPOLLIN, 1);
    
    
    	while (1) { // mainloop
    
    		struct epoll_event events[1024] = {0};
    		int nready = epoll_wait(epfd, events, 1024, -1);
    
    		int i = 0;
    		for (i = 0;i < nready;i ++) {
    
    			int connfd = events[i].data.fd;
    #if 0
    //读写二选一
    			if (events[i].events & EPOLLIN) {
    				conn_list[connfd].r_action.recv_callback(connfd);
    			} else if (events[i].events & EPOLLOUT) {
    				conn_list[connfd].send_callback(connfd);
    			}
    
    #else 
    // 读写共存
    			if (events[i].events & EPOLLIN) {
    				conn_list[connfd].r_action.recv_callback(connfd);
    			} 
    
    			if (events[i].events & EPOLLOUT) {
    				conn_list[connfd].send_callback(connfd);
    			}
    #endif
    
            }
        }
    
    
    }

    ​ 然后编译

    gcc -o reactor reactor.c
    ./reactor
    
  2. 调试

    1. 打开网络调试助手!------tcp client 改port addr
    2. 一直连接报segmentation fault
  3. 总结

    1. fd:对于client recv; 对于server是accept
    2. epoll与reactor区别
      1. reactor:读recv写send分开;每个io封装独立
      2. epoll:不能拿连续收存储

百万并发

没装3个client

看了一遍, reactor老是报错segmentation fault

这部分 跳了,后面再看吧,现在也没看懂,乱七八糟的

遇到问题不要慌,小问题

  1. too many open files:ulimit -n 1048576
  2. 分配ip不够
相关推荐
cuisidong199722 分钟前
如何在 Kali Linux 上安装 Google Chrome 浏览器
linux·运维·chrome
凌云行者29 分钟前
使用rust写一个Web服务器——单线程版本
服务器·前端·rust
光通信学徒1 小时前
ubuntu图形界面右上角网络图标找回解决办法
linux·服务器·ubuntu·信息与通信·模块测试
wusam1 小时前
螺蛳壳里做道场:老破机搭建的私人数据中心---Centos下Docker学习03(网络及IP规划)
运维·服务器·网络·docker·容器
你会发光哎u1 小时前
Webpack模式-Resolve-本地服务器
服务器·前端·webpack
南种北李1 小时前
Linux自动化构建工具Make/Makefile
linux·运维·自动化
一直在进步的派大星1 小时前
Docker 从安装到实战
java·运维·docker·微服务·容器
小飞猪Jay1 小时前
面试速通宝典——10
linux·服务器·c++·面试
哲伦贼稳妥2 小时前
一天认识一个硬件之电源
运维·其他·电脑·硬件工程
暗恋 懒羊羊2 小时前
Linux 生产者消费者模型
linux·开发语言·ubuntu