diff options
| author | verdant <im@verdant.ee> | 2026-06-09 16:31:17 +0800 |
|---|---|---|
| committer | verdant <im@verdant.ee> | 2026-06-09 16:31:17 +0800 |
| commit | 61f87eee3d57f8686cbf5e6b139e1b6c41f1141b (patch) | |
| tree | 454167dda36d02643893e1ba4c99e05b584d042e /server.c | |
| parent | 60754acffd1a00341d0518c2274284d63e0724a3 (diff) | |
| download | shsd-61f87eee3d57f8686cbf5e6b139e1b6c41f1141b.tar.gz shsd-61f87eee3d57f8686cbf5e6b139e1b6c41f1141b.zip | |
Use sendfile() instead foreach to send single char
Diffstat (limited to 'server.c')
| -rw-r--r-- | server.c | 13 |
1 files changed, 10 insertions, 3 deletions
@@ -7,6 +7,8 @@ #include <string.h> #include <sys/socket.h> #include <sys/types.h> +#include <sys/stat.h> +#include <sys/sendfile.h> #include <unistd.h> int main(int argc, char **argv) @@ -70,10 +72,15 @@ int main(int argc, char **argv) strlen(http_content_type), 0); FILE *index_fd = fopen("./index.html", "r"); - char c; - while ((c = getc(index_fd)) != EOF) { - send(new_fd, &c, 1, 0); + + struct stat stat_buf; + int stat_result = fstat(fileno(index_fd), &stat_buf); + if (stat_result != 0) { + perror("fstat"); } + + sendfile(new_fd, fileno(index_fd), NULL, + stat_buf.st_size); } memset(buf, 0, 1024); |
