Last active
August 29, 2015 14:07
-
-
Save CatTail/62201b7e1339d764e280 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <netinet/in.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <fcntl.h> | |
#include <sys/epoll.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <errno.h> | |
#include <resolv.h> | |
#include <arpa/inet.h> | |
#include <errno.h> | |
#include <stdlib.h> | |
#define MAX_EVENTS 10 | |
#define MY_PORT 8080 | |
#define MAXBUF 1024 | |
void setNonblocking(int fd) | |
{ | |
int flags = fcntl(fd, F_GETFL, 0); | |
fcntl(fd, F_SETFL, flags | O_NONBLOCK); | |
} | |
int main() | |
{ | |
struct epoll_event ev, events[MAX_EVENTS]; | |
int listen_sock, conn_sock, nfds, epollfd, n; | |
struct sockaddr_in self; | |
struct sockaddr_in local; | |
int addrlen = sizeof(local); | |
char buffer[MAXBUF]; | |
/* Set up listening socket, 'listen_sock' (socket(), | |
bind(), listen()) */ | |
if ( (listen_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) | |
{ | |
perror("Socket"); | |
exit(errno); | |
} | |
bzero(&self, sizeof(self)); | |
self.sin_family = AF_INET; | |
self.sin_port = htons(MY_PORT); | |
self.sin_addr.s_addr = INADDR_ANY; | |
if ( bind(listen_sock, (struct sockaddr*)&self, sizeof(self)) != 0 ) | |
{ | |
perror("socket--bind"); | |
exit(errno); | |
} | |
if ( listen(listen_sock, 20) != 0 ) | |
{ | |
perror("socket--listen"); | |
exit(errno); | |
} | |
epollfd = epoll_create(10); | |
if (epollfd == -1) { | |
perror("epoll_create"); | |
exit(EXIT_FAILURE); | |
} | |
ev.events = EPOLLIN; | |
ev.data.fd = listen_sock; | |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == -1) { | |
perror("epoll_ctl: listen_sock"); | |
exit(EXIT_FAILURE); | |
} | |
for (;;) { | |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); | |
if (nfds == -1) { | |
close(listen_sock); | |
perror("epoll_pwait"); | |
exit(EXIT_FAILURE); | |
} | |
for (n = 0; n < nfds; ++n) { | |
if (events[n].data.fd == listen_sock) { | |
conn_sock = accept(listen_sock, | |
(struct sockaddr *) &local, &addrlen); | |
if (conn_sock == -1) { | |
perror("accept"); | |
exit(EXIT_FAILURE); | |
} | |
setNonblocking(conn_sock); | |
ev.events = EPOLLIN | EPOLLET; | |
ev.data.fd = conn_sock; | |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock, &ev) == -1) { | |
perror("epoll_ctl: conn_sock"); | |
exit(EXIT_FAILURE); | |
} | |
} else { | |
conn_sock = events[n].data.fd; | |
printf("%s:%d connected\n", inet_ntoa(local.sin_addr), ntohs(local.sin_port)); | |
send(conn_sock, buffer, recv(conn_sock, buffer, MAXBUF, 0), 0); | |
if (epoll_ctl(epollfd, EPOLL_CTL_DEL, conn_sock, &ev) == -1) { | |
perror("epoll_ctl: conn_sock"); | |
exit(EXIT_FAILURE); | |
} | |
close(conn_sock); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment