|
| 1 | +/* |
| 2 | + * Attention: |
| 3 | + * To keep things simple, do not handle socket/bind/listen/.../epoll_create/epoll_wait API error |
| 4 | + */ |
| 5 | +#include <arpa/inet.h> |
| 6 | +#include <sys/types.h> |
| 7 | +#include <sys/socket.h> |
| 8 | +#include <netdb.h> |
| 9 | +#include <string.h> |
| 10 | +#include <unistd.h> |
| 11 | +#include <fcntl.h> |
| 12 | +#include <sys/epoll.h> |
| 13 | +#include <errno.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | + |
| 17 | +#define DEFAULT_PORT 8000 |
| 18 | +#define MAX_CONN 16 |
| 19 | +#define MAX_EVENTS 32 |
| 20 | +#define BUF_SIZE 16 |
| 21 | +#define MAX_LINE 256 |
| 22 | + |
| 23 | +void server_run(); |
| 24 | +void client_run(); |
| 25 | + |
| 26 | +int main(int argc, char *argv[]) |
| 27 | +{ |
| 28 | + int opt; |
| 29 | + char role = 's'; |
| 30 | + while ((opt = getopt(argc, argv, "cs")) != -1) { |
| 31 | + switch (opt) { |
| 32 | + case 'c': |
| 33 | + role = 'c'; |
| 34 | + break; |
| 35 | + case 's': |
| 36 | + break; |
| 37 | + default: |
| 38 | + printf("usage: %s [-cs]\n", argv[0]); |
| 39 | + exit(1); |
| 40 | + } |
| 41 | + } |
| 42 | + if (role == 's') { |
| 43 | + server_run(); |
| 44 | + } else { |
| 45 | + client_run(); |
| 46 | + } |
| 47 | + return 0; |
| 48 | +} |
| 49 | + |
| 50 | +/* |
| 51 | + * register events of fd to epfd |
| 52 | + */ |
| 53 | +static void epoll_ctl_add(int epfd, int fd, uint32_t events) |
| 54 | +{ |
| 55 | + struct epoll_event ev; |
| 56 | + ev.events = events; |
| 57 | + ev.data.fd = fd; |
| 58 | + if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) == -1) { |
| 59 | + perror("epoll_ctl()\n"); |
| 60 | + exit(1); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +static void set_sockaddr(struct sockaddr_in *addr) |
| 65 | +{ |
| 66 | + bzero((char *)addr, sizeof(struct sockaddr_in)); |
| 67 | + addr->sin_family = AF_INET; |
| 68 | + addr->sin_addr.s_addr = INADDR_ANY; |
| 69 | + addr->sin_port = htons(DEFAULT_PORT); |
| 70 | +} |
| 71 | + |
| 72 | +static int setnonblocking(int sockfd) |
| 73 | +{ |
| 74 | + if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | O_NONBLOCK) == |
| 75 | + -1) { |
| 76 | + return -1; |
| 77 | + } |
| 78 | + return 0; |
| 79 | +} |
| 80 | + |
| 81 | +/* |
| 82 | + * epoll echo server |
| 83 | + */ |
| 84 | +void server_run() |
| 85 | +{ |
| 86 | + int i; |
| 87 | + int n; |
| 88 | + int epfd; |
| 89 | + int nfds; |
| 90 | + int listen_sock; |
| 91 | + int conn_sock; |
| 92 | + int socklen; |
| 93 | + char buf[BUF_SIZE]; |
| 94 | + struct sockaddr_in srv_addr; |
| 95 | + struct sockaddr_in cli_addr; |
| 96 | + struct epoll_event events[MAX_EVENTS]; |
| 97 | + |
| 98 | + listen_sock = socket(AF_INET, SOCK_STREAM, 0); |
| 99 | + |
| 100 | + set_sockaddr(&srv_addr); |
| 101 | + bind(listen_sock, (struct sockaddr *)&srv_addr, sizeof(srv_addr)); |
| 102 | + |
| 103 | + setnonblocking(listen_sock); |
| 104 | + listen(listen_sock, MAX_CONN); |
| 105 | + |
| 106 | + epfd = epoll_create(1); |
| 107 | + epoll_ctl_add(epfd, listen_sock, EPOLLIN | EPOLLOUT | EPOLLET); |
| 108 | + |
| 109 | + socklen = sizeof(cli_addr); |
| 110 | + int a; |
| 111 | + for (a = 0; a < 2; a++) { |
| 112 | + nfds = epoll_wait(epfd, events, MAX_EVENTS, -1); |
| 113 | + for (i = 0; i < nfds; i++) { |
| 114 | + if (events[i].data.fd == listen_sock) { |
| 115 | + /* handle new connection */ |
| 116 | + conn_sock = |
| 117 | + accept(listen_sock, |
| 118 | + (struct sockaddr *)&cli_addr, |
| 119 | + &socklen); |
| 120 | + |
| 121 | + inet_ntop(AF_INET, (char *)&(cli_addr.sin_addr), |
| 122 | + buf, sizeof(cli_addr)); |
| 123 | + printf("[+] connected with %s:%d\n", buf, |
| 124 | + ntohs(cli_addr.sin_port)); |
| 125 | + |
| 126 | + setnonblocking(conn_sock); |
| 127 | + epoll_ctl_add(epfd, conn_sock, |
| 128 | + EPOLLIN | EPOLLET | EPOLLRDHUP | |
| 129 | + EPOLLHUP); |
| 130 | + } else if (events[i].events & EPOLLIN) { |
| 131 | + /* handle EPOLLIN event */ |
| 132 | + for (;;) { |
| 133 | + bzero(buf, sizeof(buf)); |
| 134 | + n = read(events[i].data.fd, buf, |
| 135 | + sizeof(buf)); |
| 136 | + if (n <= 0 /* || errno == EAGAIN */ ) { |
| 137 | + break; |
| 138 | + } else { |
| 139 | + printf("[+] data: %s\n", buf); |
| 140 | + write(events[i].data.fd, buf, |
| 141 | + strlen(buf)); |
| 142 | + } |
| 143 | + } |
| 144 | + } else { |
| 145 | + printf("[+] unexpected\n"); |
| 146 | + } |
| 147 | + /* check if the connection is closing */ |
| 148 | + if (events[i].events & (EPOLLRDHUP | EPOLLHUP)) { |
| 149 | + printf("[+] connection closed\n"); |
| 150 | + epoll_ctl(epfd, EPOLL_CTL_DEL, |
| 151 | + events[i].data.fd, NULL); |
| 152 | + close(events[i].data.fd); |
| 153 | + continue; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +/* |
| 160 | + * test clinet |
| 161 | + */ |
| 162 | +void client_run() |
| 163 | +{ |
| 164 | + int n; |
| 165 | + int c; |
| 166 | + int sockfd; |
| 167 | + char buf[MAX_LINE]; |
| 168 | + struct sockaddr_in srv_addr; |
| 169 | + |
| 170 | + sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 171 | + |
| 172 | + set_sockaddr(&srv_addr); |
| 173 | + |
| 174 | + if (connect(sockfd, (struct sockaddr *)&srv_addr, sizeof(srv_addr)) < 0) { |
| 175 | + perror("connect()"); |
| 176 | + exit(1); |
| 177 | + } |
| 178 | + |
| 179 | + for (;;) { |
| 180 | + printf("input: "); |
| 181 | + fgets(buf, sizeof(buf), stdin); |
| 182 | + c = strlen(buf) - 1; |
| 183 | + buf[c] = '\0'; |
| 184 | + write(sockfd, buf, c + 1); |
| 185 | + |
| 186 | + bzero(buf, sizeof(buf)); |
| 187 | + while (errno != EAGAIN |
| 188 | + && (n = read(sockfd, buf, sizeof(buf))) > 0) { |
| 189 | + printf("echo: %s\n", buf); |
| 190 | + bzero(buf, sizeof(buf)); |
| 191 | + |
| 192 | + c -= n; |
| 193 | + if (c <= 0) { |
| 194 | + break; |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + close(sockfd); |
| 199 | +} |
0 commit comments