zoukankan      html  css  js  c++  java
  • socke+epoll

    读:

        #define V5CLI_READ_MSG_LEN 1024
        char readMsg[V5CLI_READ_MSG_LEN];
        int n = 0;
        int nread = 0;
        while((nread = read(eventfd, readMsg + n, V5CLI_READ_MSG_LEN-1)) > 0){                    
            n += nread;                
        }//读到EAGAIN,说明读完了
        if(nread < 0 && errno != EAGAIN) {
            V5CLI_LOG_ERROR("v5cli process read err(%d)",errno);
            close(eventfd);
            eventList[loop].data.fd = -1;
            continue;
        }

    写:

        unsigned long data_size = VOS_StrLen(pszString);
        int n = data_size;
        int nwrite = 0;
        while (n > 0) {
            nwrite = write(connect_fd, (char *)(pszString + data_size-n), n);
            if (nwrite < n) {
                if (nwrite == -1 && errno != EAGAIN) {
                    perror("write error");
                }
                break;
            }
            n -= nwrite;
        }

    accept:

        if(eventList[loop].data.fd == serverfd){
            clientLen = sizeof(struct sockaddr_un);
            memset(&client, sizeof(struct sockaddr_un), 0, sizeof(struct sockaddr_un));
            while((conn_sock = accept(serverfd, (struct sockaddr*)&client, &clientLen)) > 0)
            {
                /* 将socket设置为非阻塞模式 */
                if(nonBlockSocket(conn_sock) != V5CLI_OK) {
                    printf("v5cli process nonblock failed.
    ");
                }
                event.data.fd = conn_sock;
                event.events = EPOLLIN|EPOLLET;
                printf("v5cli process new client(%d)
    ",conn_sock);
                epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock,&event); //将新的conn_sock添加到epoll的监听队列中
                memset(&client, sizeof(struct sockaddr_un), 0, sizeof(struct sockaddr_un));
                
                /*if(nonBlockSocket(conn_sock) != V5CLI_OK) {
                    printf("v5cli process set non block fail");
                    goto END_PRO;
                }*/
            }
            if(conn_sock < 0 && errno != EAGAIN) {
                printf("v5cli process accept err %d", conn_sock);
            }
        }
  • 相关阅读:
    eclipse下jsp文件报错解决方法
    使用vscode搭建本地的websocket
    tomcat的首次登录配置
    tomcat配置报错解决方法 The jre_home environment variable is not defined correctly
    cento升级openssl依旧显示老版本
    Centos6安装mysql5.7最新版
    Neutron服务组件
    网络OSI 7层模型
    Kubernetes的核心技术概念和API对象
    Xen 虚拟化技术
  • 原文地址:https://www.cnblogs.com/SaraMoring/p/10410750.html
Copyright © 2011-2022 走看看