{"id":19,"date":"2021-04-15T23:23:33","date_gmt":"2021-04-15T15:23:33","guid":{"rendered":"http:\/\/139.224.63.49\/?p=19"},"modified":"2021-04-15T23:23:33","modified_gmt":"2021-04-15T15:23:33","slug":"a-daytime-server-use-epoll-lt-mode","status":"publish","type":"post","link":"http:\/\/iamnear.top\/?p=19","title":{"rendered":"a daytime server use epoll lt mode"},"content":{"rendered":"\n<p>here is the code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;unistd.h>\n#include &lt;pthread.h>\n#include &lt;arpa\/inet.h>\n#include &lt;netinet\/in.h>\n#include &lt;sys\/socket.h>\n#include &lt;sys\/epoll.h>\n#include &lt;fcntl.h>\n#include &lt;string>\n#include &lt;sys\/wait.h>\n#include &lt;iostream>\n#include &lt;syslog.h>\n#include &lt;signal.h>\nusing std::cout;\nusing std::endl;\nbool stopflag = false;\nvoid handlesignal(int signum)\n{\n    if(signum==SIGTERM)\n    {\n        stopflag = true;\n    }\n}\nint setnonblocking(int fd)\n{\n    int old_option = fcntl(fd,F_GETFL);\n    int new_option = old_option|O_NONBLOCK;\n    fcntl(fd,F_SETFL,new_option);\n    return old_option;\n}\nvoid addtoepoll(int epfd,int fd)\n{\n    setnonblocking(fd);\n    epoll_event event;\n    event.events = EPOLLIN;\n    event.data.fd = fd;\n    epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&amp;event);\n}\nstd::string getdatestring()\n{\n    int p&#91;2];\n    pipe(p);\n    int ret;\n    if((ret=fork())==0)\n    {\n        close(STDOUT_FILENO);\n        dup(p&#91;1]);\n        execl(\"\/usr\/bin\/date\",\"date\",nullptr);\n        exit(-1);\n    }\n    else\n    {\n        waitpid(ret,nullptr,0);\n        char buf&#91;500];\n        int l = read(p&#91;0],buf,500);\n        return std::string(buf,l);\n    }\n}\nvoid set_reuse_addr(int fd)\n{\n    int option = 1;\n    setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&amp;option,sizeof(option));\n}\nint main()\n{\n    daemon(0,0);\n    signal(SIGTERM,handlesignal);\n    openlog(\"my_daytime_server\",LOG_PID,LOG_USER);\n    setlogmask(LOG_UPTO(LOG_DEBUG));\n    int listen_tcp_fd = socket(AF_INET,SOCK_STREAM,0);\n    int listen_udp_fd = socket(AF_INET,SOCK_DGRAM,0);\n    set_reuse_addr(listen_tcp_fd);\n    set_reuse_addr(listen_udp_fd);\n    if(listen_tcp_fd&lt;0||listen_udp_fd&lt;0)\n    {\n        perror(\"create socket failed\");\n        return -1;\n    }\n    sockaddr_in local_addr;\n    local_addr.sin_port = htons(13);\n    local_addr.sin_family=AF_INET;\n    inet_aton(\"0.0.0.0\",&amp;local_addr.sin_addr);\n    int ret = bind(listen_tcp_fd,(sockaddr*)&amp;local_addr,sizeof(local_addr));\n    if(ret&lt;0)\n    {\n        perror(\"bind tcp socket failed\");\n        return -1;\n    }\n    ret = bind(listen_udp_fd,(sockaddr*)&amp;local_addr,sizeof(local_addr));\n    if(ret&lt;0)\n    {\n        perror(\"bind udp socket failed\");\n        return -1;\n    }\n    listen(listen_tcp_fd,5);\n    int epollfd = epoll_create(1);\n    addtoepoll(epollfd,listen_tcp_fd);\n    addtoepoll(epollfd,listen_udp_fd);\n    epoll_event events&#91;20];\n\n    while(!stopflag)\n    {\n        ret = epoll_wait(epollfd,events,20,-1);\n        for(int i=0;i&lt;ret;i++)\n        {\n            int fd = events&#91;i].data.fd;\n            sockaddr_in client_addr;\n            socklen_t length = sizeof(client_addr);\n            std::string s = getdatestring();\n            if(fd==listen_tcp_fd)\n            {\n                int client_fd = accept(fd,(sockaddr*)&amp;client_addr,&amp;length);\n                syslog(LOG_USER|LOG_INFO,\"recv tcp connection from %s:%d\",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));\n                send(client_fd,s.c_str(),s.length(),0);\n                close(client_fd);\n            }\n            else if(fd==listen_udp_fd)\n            {\n                char buffer&#91;1024];\n                recvfrom(listen_udp_fd,buffer,1024,0,(sockaddr*)&amp;client_addr,&amp;length);\n                syslog(LOG_USER|LOG_INFO,\"recv udp package from %s:%d\",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));\n                sendto(listen_udp_fd,s.c_str(),s.length(),0,(sockaddr*)&amp;client_addr,length);\n            }\n        }\n    }\n    closelog();\n    syslog(LOG_USER|LOG_INFO,\"server exit normally by sigterm signal\");\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>since i&#8217;m not familar with the behavior of accept function under et mode, so i choose lt mode. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>here is the code since i&#8217;m not familar with &hellip; <a href=\"http:\/\/iamnear.top\/?p=19\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">a daytime server use epoll lt mode<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":{"0":"post-19","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-c_plus_plus","7":"h-entry","9":"h-as-article"},"_links":{"self":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19"}],"version-history":[{"count":1,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":20,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions\/20"}],"wp:attachment":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}