Skip to content

Instantly share code, notes, and snippets.

@nir9
Last active March 27, 2024 21:58
Show Gist options
  • Save nir9/2db83a89c18a8add1e705710e9303208 to your computer and use it in GitHub Desktop.
Save nir9/2db83a89c18a8add1e705710e9303208 to your computer and use it in GitHub Desktop.
Minimalist Web Server in C on Windows (using winsock) - only for fun, not for production use
#include <winsock2.h>
#include <Windows.h>
#include <stdio.h>
int main()
{
WSADATA wsadata;
WSAStartup(MAKEWORD(2, 2), &wsadata);
SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = 0;
addr.sin_port = htons(8080);
bind(s, &addr, sizeof(addr));
listen(s, 10);
SOCKET client = accept(s, 0, 0);
char request[256] = {0};
recv(client, request, 256, 0);
// GET /[file] ...
if (memcmp(request, "GET / ", 6) == 0) {
FILE* f = fopen("index.html", "r");
char buffer[256] = {0};
fread(buffer, 1, 256, f);
send(client, buffer, 256, 0);
}
}
@nir9
Copy link
Author

nir9 commented Mar 25, 2024

index.html

HTTP/1.0 200 OK
Content-type: text/html

Hello from our C server! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment