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
| //tcp.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <errno.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <fcntl.h> #include <sys/epoll.h> #include <pthread.h>
int createServerSocket() { int sock = socket(PF_INET, SOCK_STREAM, 0); assert(sock > 0); return sock; }
void bindSocket(int _socket, int _port) { struct sockaddr_in servaddr; memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(_port); int ret = bind(_socket, (struct sockaddr*)&servaddr, sizeof(servaddr)); assert(ret == 0); }
void make_socket_addr_reuse (int _socket) {
int optval = 1; int ret1 = setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); int ret2 = setsockopt(_socket, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));
assert(ret1 == 0); assert(ret2 == 0);
}
void listenSocket(int _socket) { listen(_socket,5); }
void make_socket_non_blocking (int _socket) { int flags = fcntl (_socket, F_GETFL, 0); assert(flags != -1);
flags |= O_NONBLOCK; int ret = fcntl (_socket, F_SETFL, flags); assert(ret == 0);
}
int createEpoll(int _size) { int epollfd = epoll_create(_size); assert(epollfd > 0); }
void epollClear(int _epoll, int fd) { int ret = epoll_ctl(_epoll, EPOLL_CTL_DEL, fd, NULL); assert(0 == ret); }
void epollAddET(int _epoll, int fd, int mask) { struct epoll_event epEvent; memset(&epEvent, 0,sizeof(epEvent)); epEvent.data.fd = fd; epEvent.events = mask | EPOLLET; int ret = epoll_ctl(_epoll, EPOLL_CTL_ADD, fd, &epEvent); assert(0 == ret); }
void epollAddLT(int _epoll, int fd, int mask) { struct epoll_event epEvent; memset(&epEvent, 0,sizeof(epEvent)); epEvent.data.fd = fd; epEvent.events = mask;
int ret = epoll_ctl(_epoll, EPOLL_CTL_ADD, fd, &epEvent); assert(0 == ret); }
int epollwait(int _epoll, struct epoll_event *events, int _maxEvents) { int ret = epoll_wait(_epoll, events, _maxEvents, -1); assert(ret > 0); return ret; }
void handleClientSock(int _epollfd, int clientSock) { char recv_buffer[2]=""; int bytes = read(clientSock, recv_buffer, sizeof(recv_buffer)); printf("recv %d byte[%s] from sock %d\n", bytes,recv_buffer, clientSock); if(bytes <= 0){ //printf("close sock %d\n", clientSock); epollClear(_epollfd, clientSock); close(clientSock); printf("[CLOSE SOCKET %d]=====================\n",clientSock); } }
void handleAcceptSock(int _epollfd, int serverSock) { struct sockaddr_in client_addr; socklen_t client_len = sizeof(client_addr); int client = accept(serverSock, (struct sockaddr *)&client_addr, &client_len);
assert(client > 0); epollAddLT(_epollfd, client, EPOLLIN); printf("[ACCEPT SOCKET %d]=====================\n",client); }
void* masterThreadBody(void *arg) { int epollfd = createEpoll(100); int tcpsocket = (int)arg; printf("[epoll thread create. sock = %d]\n", tcpsocket); epollAddET(epollfd, tcpsocket, EPOLLIN); struct epoll_event fired_events[100]; while(1) { int eventCount = epollwait(epollfd, fired_events, 100); assert(eventCount > 0); int i=0; for(;i<eventCount;i++) { printf("[epoll awake. event sock = %d]\n", fired_events[i].data.fd); if(fired_events[i].data.fd == tcpsocket){ handleAcceptSock(epollfd,fired_events[i].data.fd); }else{ handleClientSock(epollfd,fired_events[i].data.fd); } }
} return NULL; }
pthread_t createMasterThread(int tcpSock) { pthread_t tid; int ret = pthread_create(&tid,NULL,masterThreadBody,(void*)tcpSock); assert(ret == 0); return tid; }
int main(int argc, char** argv) { int sock = createServerSocket(); make_socket_addr_reuse(sock); bindSocket(sock, 666); listenSocket(sock); pthread_t tid = createMasterThread(sock); pthread_join(tid, NULL); }
|