Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <memory.h>
- #include <netdb.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/epoll.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- int const MAX_EVENTS = 1024;
- static int create_and_bind(char const* port)
- {
- struct addrinfo hints;
- struct addrinfo* result,* rp;
- int s, sfd;
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
- hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
- hints.ai_flags = AI_PASSIVE; /* All interfaces */
- s = getaddrinfo(NULL, port, &hints, &result);
- if (s != 0) {
- fprintf(stderr, "getaddrinfo: %s\n", gai_strerror (s));
- return -1;
- }
- for (rp = result; rp != NULL; rp = rp->ai_next) {
- sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
- if (sfd == -1)
- continue;
- s = bind(sfd, rp->ai_addr, rp->ai_addrlen);
- if (s == 0) {
- break;
- }
- close (sfd);
- }
- if (rp == NULL) {
- fprintf (stderr, "Could not bind\n");
- return -1;
- }
- freeaddrinfo(result);
- return sfd;
- }
- static int make_socket_non_blocking(int fd)
- {
- int flags, s;
- flags = fcntl(fd, F_GETFL, 0);
- if (flags == -1) {
- perror ("fcntl");
- return -1;
- }
- flags |= O_NONBLOCK;
- s = fcntl(fd, F_SETFL, flags);
- if (s == -1) {
- perror ("fcntl");
- return -1;
- }
- return 0;
- }
- int main() {
- int epfd, i, n;
- struct epoll_event events[MAX_EVENTS];
- struct epoll_event event;
- int sockfd = create_and_bind("4999");
- if (sockfd == -1) {
- perror("create and bind");
- return 1;
- }
- if (make_socket_non_blocking(sockfd)) {
- perror("mask non blocking");
- return 1;
- }
- if (listen(sockfd, 1024)) {
- perror("listen");
- return 1;
- }
- epfd = epoll_create(1024);
- if (epfd == -1) {
- perror("epoll create");
- return 1;
- }
- event.data.fd = sockfd;
- event.events = EPOLLIN | EPOLLET;
- if (epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event)) {
- perror("epoll ctl");
- return 1;
- }
- while (1) {
- n = epoll_wait(epfd, events, MAX_EVENTS, -1);
- for (i = 0; i < n; ++i) {
- // for (i = 0; i < epoll_wait(epfd, events, MAX_EVENTS, -1); ++i) {
- if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) {
- fprintf(stderr, "epoll error\n");
- close(events[i].data.fd);
- continue;
- }
- if (events[i].data.fd == sockfd) {
- while (1) {
- struct sockaddr in_addr;
- socklen_t in_len;
- int infd;
- char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
- in_len = sizeof in_addr;
- infd = accept (sockfd, &in_addr, &in_len);
- if (infd == -1) {
- if ((errno == EAGAIN) ||
- (errno == EWOULDBLOCK)) {
- break;
- } else {
- perror ("accept");
- break;
- }
- }
- if (getnameinfo (&in_addr, in_len,
- hbuf, sizeof hbuf,
- sbuf, sizeof sbuf,
- NI_NUMERICHOST | NI_NUMERICSERV) == 0)
- {
- printf("Accepted connection on descriptor %d "
- "(host=%s, port=%s)\n", infd, hbuf, sbuf);
- }
- if (make_socket_non_blocking (infd))
- return 1;
- event.data.fd = infd;
- event.events = EPOLLIN | EPOLLET;
- if (epoll_ctl (epfd, EPOLL_CTL_ADD, infd, &event) == -1) {
- perror ("epoll_ctl");
- return 1;
- }
- }
- continue;
- }
- int done = 0;
- while (1) {
- ssize_t count;
- char buf[512];
- printf("Reading...\n");
- fflush(stdout);
- count = read(events[i].data.fd, buf, sizeof buf - 1);
- if (count == -1) {
- if (errno != EAGAIN) {
- perror("read");
- done = 1;
- }
- break;
- }
- printf("%d bytes read\n", count);
- buf[count] = 0;
- printf("Message: %s\n", buf);
- if (count < sizeof buf) {
- done = 1;
- break;
- }
- fflush(stdout);
- }
- if (done) {
- printf ("Closed connection on descriptor %d\n",
- events[i].data.fd);
- close (events[i].data.fd);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement