1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
| #define _GNU_SOURCE 1 #include <sys/epoll.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <fcntl.h>
#define MAX_USER_NUM 10 #define MAX_FDS_NUM 1024 #define MAX_BUF_SIZE 512 #define MAX_EVENT_NUM 10
typedef struct _userInf { struct sockaddr_in addr; char wrBuf[MAX_BUF_SIZE]; char rdBuf[MAX_BUF_SIZE]; } userData;
int setnonblocking(int fd) { int old_option = fcntl( fd, F_GETFL ); int new_option = old_option | O_NONBLOCK; fcntl( fd, F_SETFL, new_option ); return old_option; }
int main(int argc, char **argv) { int32_t status;
if (argc <= 1) { printf("Please enter 2 arguments...\n"); return argc; }
const char *ipAddr = "127.0.0.1"; uint32_t port = atoi(argv[1]); struct sockaddr_in listenSockAddr; bzero(&listenSockAddr, sizeof(listenSockAddr)); listenSockAddr.sin_family = AF_INET; listenSockAddr.sin_port = htons(port); inet_pton(AF_INET, ipAddr, &listenSockAddr.sin_addr); int32_t listenFd = socket(AF_INET, SOCK_STREAM, 0); if (listenFd < 0) { printf("listenFd = %d...\n", listenFd); return listenFd; } status = bind(listenFd, (sockaddr *)&listenSockAddr, sizeof(listenSockAddr)); if (status < 0) { printf("bind status = %d...\n", status); return status; } status = listen(listenFd, 20); if (status < 0) { printf("listen status = %d...\n", status); return status; } setnonblocking(listenFd);
struct epoll_event eplistenEvents, recEvents[MAX_EVENT_NUM]; int32_t epFd = epoll_create(1); eplistenEvents.data.fd = listenFd; eplistenEvents.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLET; status = epoll_ctl(epFd, EPOLL_CTL_ADD, listenFd, &eplistenEvents); if (status < 0) { printf("epoll_ctl status = %d...\n", status); return status; }
userData *users = new userData[MAX_FDS_NUM]; users[listenFd].addr = listenSockAddr; int connectedUser[MAX_USER_NUM]; int connectedUserNum = 0; for (int i = 0; i < MAX_USER_NUM; i++) { connectedUser[i] = -1; } while (1) { int32_t rdyFdCnt = epoll_wait(epFd, recEvents, MAX_EVENT_NUM, -1); if (rdyFdCnt < 0) { printf("epoll_wait fail...\n"); return rdyFdCnt; }
for (int i = 0; i < rdyFdCnt; i++) { uint32_t events = recEvents[i].events; if (events & EPOLLERR) { printf("error in recEvents...\n"); return EPOLLERR; } else if (recEvents[i].data.fd == listenFd) { while (1) { struct sockaddr cliSockAddr; socklen_t addrLen = sizeof(cliSockAddr); bzero(&cliSockAddr, addrLen); int cliFd = accept(listenFd, &cliSockAddr, &addrLen); if (cliFd < 0) { if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { printf("accept error...\n"); return cliFd; } else { printf("all requests have been accepted...\n"); break; } } if (connectedUserNum == MAX_USER_NUM) { const char *msg = "MAX_USER_NUM is reached...\n"; printf("%s", msg); send(cliFd ,msg, strlen(msg), 0); close(cliFd); break; } setnonblocking(cliFd); struct epoll_event epCliEvents; epCliEvents.data.fd = cliFd; epCliEvents.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLET; status = epoll_ctl(epFd, EPOLL_CTL_ADD, cliFd, &epCliEvents); if (status < 0) { printf("epoll_ctl for new clients error...\n"); return status; } users[cliFd].addr = *(struct sockaddr_in *)&cliSockAddr; connectedUser[connectedUserNum] = cliFd; connectedUserNum++; } } else if (events & EPOLLRDHUP) { int closeFd = recEvents[i].data.fd; printf("fd = %d left...\n", closeFd); close(closeFd); bzero(&users[closeFd], sizeof(userData)); connectedUser[closeFd] = connectedUser[connectedUserNum - 1]; connectedUser[connectedUserNum - 1] = -1; connectedUserNum--; } else if (events & EPOLLIN) { int cliFd = recEvents[i].data.fd; bzero(users[cliFd].rdBuf, MAX_BUF_SIZE); while (1) { ssize_t rdLen = read(cliFd, users[cliFd].rdBuf, MAX_BUF_SIZE); if (rdLen < 0) { if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { printf("socket read error...\n"); close(cliFd); bzero(&users[cliFd], sizeof(users[0])); for (int i = 0; i < MAX_USER_NUM; i++) { if (connectedUser[i] == cliFd) { connectedUser[i] = connectedUser[connectedUserNum - 1]; connectedUser[connectedUserNum - 1] = -1; break; } } connectedUserNum--; } else { break; } } else if (rdLen > 0) { for (int i = 0; i < connectedUserNum; i++) { struct epoll_event epCliEvents; epCliEvents.data.fd = connectedUser[i]; epCliEvents.events = EPOLLOUT | EPOLLERR | EPOLLET; status = epoll_ctl(epFd, EPOLL_CTL_MOD, connectedUser[i], &epCliEvents); if (status < 0) { printf("epoll_ctl for existing clients error...\n"); close(connectedUser[i]); bzero(&users[cliFd], sizeof(users[0])); connectedUser[i] = connectedUser[connectedUserNum - 1]; connectedUser[connectedUserNum - 1] = -1; connectedUserNum--; i--; continue; } memcpy(users[connectedUser[i]].wrBuf, users[cliFd].rdBuf, strlen(users[cliFd].rdBuf)); } } else { printf("read 0, disconnect from client...\n"); } } } else if (events & EPOLLOUT) { int outputFd = recEvents[i].data.fd; size_t wrLen = MAX_BUF_SIZE; ssize_t sentCnt; while (1) { if (wrLen == 0) { struct epoll_event epCliEvents; epCliEvents.data.fd = outputFd; epCliEvents.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLET; int status = epoll_ctl(epFd, EPOLL_CTL_MOD, outputFd, &epCliEvents); if (status < 0) { printf("epoll_ctl error...\n"); return status; } bzero(users[outputFd].wrBuf, MAX_BUF_SIZE); break; } sentCnt = send(outputFd, users[outputFd].wrBuf, wrLen, 0); if (sentCnt < 0) { printf("send data error...\n"); continue; } wrLen -= sentCnt; } } } }
return 0; }
|