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
| #include <sys/socket.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #include <stdio.h> #include <arpa/inet.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/sendfile.h> #include <sys/epoll.h>
#define PORT 8080 #define MAX_EVENTS 1024
static void set_nonblock(int fd) { int flags = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, flags | O_NONBLOCK); }
int main() { int listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (listen_fd < 0) { perror("socket"); return 1; }
int opt = 1; setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(PORT); addr.sin_addr.s_addr = INADDR_ANY;
if (bind(listen_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { perror("bind"); return 1; }
if (listen(listen_fd, SOMAXCONN) < 0) { perror("listen"); return 1; }
printf("Server running on http://0.0.0.0:%d\n", PORT);
set_nonblock(listen_fd);
int epfd = epoll_create1(0); struct epoll_event ev, events[MAX_EVENTS];
ev.events = EPOLLIN; ev.data.fd = listen_fd; epoll_ctl(epfd, EPOLL_CTL_ADD, listen_fd, &ev);
while (1) { int n = epoll_wait(epfd, events, MAX_EVENTS, -1); for (int i = 0; i < n; i++) { int fd = events[i].data.fd;
if (fd == listen_fd) { int client_fd = accept(listen_fd, NULL, NULL); if (client_fd >= 0) { set_nonblock(client_fd); ev.events = EPOLLIN; ev.data.fd = client_fd; epoll_ctl(epfd, EPOLL_CTL_ADD, client_fd, &ev); } continue; }
if (events[i].events & EPOLLIN) { char buffer[2048] = {0}; ssize_t nread = recv(fd, buffer, sizeof(buffer), 0);
if (nread <= 0) { close(fd); epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); continue; }
int file_fd = open("index.html", O_RDONLY); if (file_fd < 0) { const char *err = "HTTP/1.1 404 Not Found\r\n" "Content-Type: text/plain\r\n" "Content-Length: 13\r\n" "Connection: close\r\n\r\n" "404 Not Found"; send(fd, err, strlen(err), 0); close(fd); epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); continue; }
struct stat st; fstat(file_fd, &st); size_t filesize = st.st_size;
char header[256]; int len = snprintf(header, sizeof(header), "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n" "Content-Length: %zu\r\n" "Connection: close\r\n\r\n", filesize ); send(fd, header, len, 0);
off_t offset = 0; while (offset < filesize) { ssize_t sent = sendfile(fd, file_fd, &offset, filesize - offset); if (sent <= 0) break; }
close(file_fd); close(fd); epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); } } }
close(listen_fd); close(epfd); return 0; }
|