欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > LINUX 精通 3.2

LINUX 精通 3.2

2024/11/29 21:46:45 来源:https://blog.csdn.net/weixin_44925529/article/details/139736914  浏览:    关键词:LINUX 精通 3.2
  1. main里怎么实现

    accept_cb 里的regist部分抽出来

    // 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里

    
    int main(){unsigned short port = 2000;int sockfd = init_server(port);epfd = epoll_create(1);set_event(sockfd, EPOLLIN);while (1) { // mainloopstruct 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

    #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 * 1024int 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是fdchar 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};
    // fdint init_server(unsigned short port){// 创建抄network里// 创建socketint 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用eventev.data.fd = fd;epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); }else{struct epoll_event ev; //构建事件,只用来add和delete,control里没用ev.events = event;  //改这里不用epoll,reactor用eventev.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) { // disconnectprintf("client disconnect: %d\n", fd);close(fd);epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); // unfinishedreturn 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) { // mainloopstruct 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不够

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com