Skip to content

Instantly share code, notes, and snippets.

@westphahl
Created June 12, 2015 13:00
Show Gist options
  • Save westphahl/12e1e8538368a67d5eda to your computer and use it in GitHub Desktop.
Save westphahl/12e1e8538368a67d5eda to your computer and use it in GitHub Desktop.
epfd = epoll_create(1);
if (epfd == -1)
errExit("epoll_create");
ev.events = EPOLLIN;
ev.data.fd = sockfd;
epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);
ev.events = EPOLLIN;
ev.data.fd = STDIN_FILENO;
epoll_ctl(epfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev);
/* FIXME: Loop using epoll_wait() to monitor the file descriptors, and sending data from standard input to the socket and vice versa */
for (;;) {
ready = epoll_wait(epfd, &rev, 1, -1);
if (ready == -1) {
if (errno == EINTR)
continue;
else
errExit("epoll_wait");
}
if (rev.events & EPOLLIN) {
memset(inputBuf, 0, BUF_SIZE + 1);
nr = readLine(rev.data.fd, inputBuf, BUF_SIZE);
if (rev.data.fd == STDIN_FILENO) {
snprintf(outputBuf, sizeof(outputBuf), "%s: %s\n", nickname, inputBuf);
write(sockfd, outputBuf, strlen(outputBuf));
} else {
printf("%s\n", inputBuf);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment