poll listening event

  1. Create a new socket
int listenfd;
 listenfd = Socket(AF_INET,SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC,0);
  1. Bind the ip and port for the socket
sockaddr_in serv_addr;
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERV_PORT);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
Bind(listenfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
  1. Set up port multiplexing
//Set the socket of the server to support port multiplexing
 int opt = 1;
 setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
  1. Set the maximum number of listening
//Set the maximum number of listening
    Listen(listenfd,SOMAXCONN);

  1. Add listenfd to the array poll ed
 //Set the socket of the server to poll listening
    struct pollfd pfd;
    pfd.fd = listenfd;
    pfd.revents = POLLIN;

    PollFdList pollfds;
    pollfds.push_back(pfd);

  1. A dead cycle
    • Whether the linked socket has an event
    • If so, use the accept function to return the socket of the client that established the link
      • Get socket for client
      • Add the socket to the poll listening array
    • If it is the data request of the client
      • Read the contents of the socket
      • Process the contents of the socket
      • Write the processed result to the client
    //Enter the dead cycle
    while(1){

        //If an event occurs during poll listening, nready is returned
        nready = poll(&*pollfds.begin(),pollfds.size(),-1);
        if(nready == -1){
            perr_exit("poll Monitoring failed");
        }
        if(nready == 0){
            continue;
        }

        //Determine whether the socket poll ed is listenfd
        if(pollfds[0].revents & POLLIN){
            clien_addr_len = sizeof(clien_addr);

             //If so, use the accept function to accept the socket

            connfd = Accept(listenfd,(struct sockaddr*)&clien_addr,&clien_addr_len);
            //Print the ip and port number of the client that establishes the link with the server
            char client_ip[1024] = {0};
            printf("client ip:%s, client port:%d\n",
                   inet_ntop(AF_INET,&clien_addr.sin_addr.s_addr,client_ip,sizeof(client_ip)),
                   ntohs(clien_addr.sin_port));

            //Set the socket of the client's single to poll listening

            fcntl(connfd,O_NONBLOCK|O_CLOEXEC);

            pfd.fd = connfd;
            pfd.events = POLLIN;
            pollfds.push_back(pfd);
            nready--;
            if(nready == 0){
                continue;
            }

        }

        //If the socket listening to is client's

        for(PollFdList::iterator it = pollfds.begin()+1;
            it != pollfds.end() && nready > 0; it++)
        {
            if(it->revents & POLLIN){
                //Read the contents of the socket
                connfd = it->fd;
                char buf[1024] = {0};
                int len = Read(connfd,buf,sizeof(buf));
                if(len == 0){
                    std::cout<<"client close"<<std::endl;
                    pollfds.erase(it);

                    --it;
                    close(connfd);
                    continue;
                }

             //After doing some processing, write the content back to the socket of the client

                for(int i = 0; i < len; i++){
                    buf[i] = toupper(buf[i]);
                }
                Write(connfd,buf,len);
            }

        }

    }

Source code

Keywords: socket

Added by eyeself on Sun, 01 Dec 2019 10:20:23 +0200