Skip to content

Instantly share code, notes, and snippets.

@nir9
Created November 25, 2023 16:50
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save nir9/3d22d954a599a71c1ccf64ea63c4e38f to your computer and use it in GitHub Desktop.
Save nir9/3d22d954a599a71c1ccf64ea63c4e38f to your computer and use it in GitHub Desktop.
Minimalist C Web Server - not for production use, only for fun :)
#include <sys/socket.h>
#include <string.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <unistd.h>
#include <netinet/in.h>
void main() {
int s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {
AF_INET,
0x901f,
0
};
bind(s, &addr, sizeof(addr));
listen(s, 10);
int client_fd = accept(s, 0, 0);
char buffer[256] = {0};
recv(client_fd, buffer, 256, 0);
// GET /file.html .....
char* f = buffer + 5;
*strchr(f, ' ') = 0;
int opened_fd = open(f, O_RDONLY);
sendfile(client_fd, opened_fd, 0, 256);
close(opened_fd);
close(client_fd);
close(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment