<p>I need to know about Epoll On linux System.</p>

<p>Could you recommend manual or guides about epoll library?</p>

<p>need more detailed guides. it's better to have some examples.</p>

<p>help me. and Thank you for reading.</p>

[{'answer_id': 27276, 'author': 'Ryan Guest', 'author_id': 1811, 'author_profile': 'https://Stackoverflow.com/users/1811', 'pm_score': 5, 'selected': True, 'text': '<p>Here\'s an introduction to Epoll, a pretty basic tutorial: <a href="https://kovyrin.net/2006/04/13/epoll-asynchronous-network-programming/" rel="nofollow noreferrer">http://blog.kovyrin.net/2006/04/13/epoll-asynchronous-network-programming/</a></p>\n\n<p>A more complete example can be found here: <a href="https://web.archive.org/web/20120504033548/https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/" rel="nofollow noreferrer">https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/</a></p>\n\n<p>Also, <a href="https://www.man7.org/linux/man-pages/man7/epoll.7.html" rel="nofollow noreferrer">the man pages</a></p>\n'}, {'answer_id': 6150841, 'author': 'Viswesn', 'author_id': 527813, 'author_profile': 'https://Stackoverflow.com/users/527813', 'pm_score': 4, 'selected': False, 'text': '<pre><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;sys/types.h&gt;\n#include &lt;sys/socket.h&gt;\n#include &lt;netinet/in.h&gt;\n#include &lt;arpa/inet.h&gt;\n#include &lt;sys/epoll.h&gt;\n\n#define PORT 1500 \n\n#define MAX_CON (1200)\n\nstatic struct epoll_event *events;\n\nint main(int argc, char *argv[])\n{\n    fd_set master;\n    fd_set read_fds;\n    struct sockaddr_in serveraddr;\n    struct sockaddr_in clientaddr;\n    int fdmax;\n    int listener;\n    int newfd;\n    char buf[1024];\n    int nbytes;\n    int addrlen;\n    int yes;\n    int epfd = -1;\n    int res = -1;\n    struct epoll_event ev;\n    int i=0;\n    int index = 0;\n    int client_fd = -1;\n\n    int SnumOfConnection = 0;\n    time_t Sstart, Send;\n\n    if((listener = socket(AF_INET, SOCK_STREAM, 0)) == -1)\n    {\n            perror("Server-socket() error lol!");\n            exit(1);\n    }\n\n    if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &amp;yes, sizeof(int)) == -1)\n    {\n            perror("Server-setsockopt() error lol!");\n            exit(1);\n    }\n    serveraddr.sin_family = AF_INET;\n    serveraddr.sin_addr.s_addr = INADDR_ANY;\n    serveraddr.sin_port = htons(PORT);\n    memset(&amp;(serveraddr.sin_zero), \'\\0\', 8);\n    if(bind(listener, (struct sockaddr *)&amp;serveraddr, sizeof(serveraddr)) == -1)\n    {\n            perror("Server-bind() error lol!");\n            exit(1);\n    }\n    if(listen(listener, 10) == -1)\n    {\n            perror("Server-listen() error lol!");\n            exit(1);\n    }\n    fdmax = listener; /* so far, it\'s this one*/\n\n    events = calloc(MAX_CON, sizeof(struct epoll_event));\n    if ((epfd = epoll_create(MAX_CON)) == -1) {\n            perror("epoll_create");\n            exit(1);\n    }\n    ev.events = EPOLLIN;\n    ev.data.fd = fdmax;\n    if (epoll_ctl(epfd, EPOLL_CTL_ADD, fdmax, &amp;ev) &lt; 0) {\n            perror("epoll_ctl");\n            exit(1);\n    }\n    //time(&amp;start);\n    for(;;)\n    {\n            res = epoll_wait(epfd, events, MAX_CON, -1);\n            client_fd = events[index].data.fd;\n\n            for (index = 0; index &lt; MAX_CON; index++) {\n                    if(client_fd == listener)\n                    {\n                            addrlen = sizeof(clientaddr);\n                            if((newfd = accept(listener, (struct sockaddr *)&amp;clientaddr, &amp;addrlen)) == -1)\n                            {\n                                    perror("Server-accept() error lol!");\n                            }\n                            else\n                            {\n                            //      printf("Server-accept() is OK...\\n");\n                                    ev.events = EPOLLIN;\n                                    ev.data.fd = newfd;\n                                    if (epoll_ctl(epfd, EPOLL_CTL_ADD, newfd, &amp;ev) &lt; 0) {\n                                            perror("epoll_ctl");\n                                            exit(1);\n                                    }\n                            }\n                            break;\n                    }\n                    else\n                    {\n                            if (events[index].events &amp; EPOLLHUP)\n                            {\n                                    if (epoll_ctl(epfd, EPOLL_CTL_DEL, client_fd, &amp;ev) &lt; 0) {\n                                            perror("epoll_ctl");\n                                    }\n                                    close(client_fd);\n                                    break;\n                            }\n                            if (events[index].events &amp; EPOLLIN)  {\n                                    if((nbytes = recv(client_fd, buf, sizeof(buf), 0)) &lt;= 0)\n                                    {\n                                            if(nbytes == 0) {\n                                            //      printf("socket %d hung up\\n", client_fd);\n                                            }\n                                            else {\n                                                    printf("recv() error lol! %d", client_fd);\n                                                    perror("");\n                                            }\n\n                                            if (epoll_ctl(epfd, EPOLL_CTL_DEL, client_fd, &amp;ev) &lt; 0) {\n                                                    perror("epoll_ctl");\n                                            }\n                                            close(client_fd);\n                                    }\n                                    else\n                                    {\n                                            if(send(client_fd, buf, nbytes, 0) == -1)\n                                                    perror("send() error lol!");\n                                    }\n                                    break;\n                            }\n                    }\n            }\n    }\n    return 0;\n}\n</code></pre>\n\n<p>I wrote this program for testing and I was able to connect more than 80k connections and I find average system load only to 0.27%.</p>\n'}]
['https://Stackoverflow.com/questions/27247', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2370764/']