Last active
June 22, 2025 04:51
-
-
Save o-az/ff3a9b2e9bdfd74b652116d8aaba5ba4 to your computer and use it in GitHub Desktop.
simple server in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pure C REST API Server - no external dependencies | |
// Compile with: gcc -o server server.c | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <unistd.h> | |
#define PORT 8080 | |
#define BUFFER_SIZE 4096 | |
// Parse HTTP request and extract method and path | |
void parse_request(char *request, char *method, char *path) { | |
sscanf(request, "%s %s", method, path); | |
} | |
// Extract body from HTTP request | |
char* get_body(char *request) { | |
char *body = strstr(request, "\r\n\r\n"); | |
if (body) { | |
return body + 4; | |
} | |
return NULL; | |
} | |
// Handle requests | |
void handle_request(int client_socket, char *request) { | |
char method[10], path[100]; | |
char response[BUFFER_SIZE]; | |
parse_request(request, method, path); | |
// GET /ping | |
if (strcmp(method, "GET") == 0 && strcmp(path, "/ping") == 0) { | |
sprintf(response, | |
"HTTP/1.1 200 OK\r\n" | |
"Content-Type: text/plain\r\n" | |
"Content-Length: 4\r\n" | |
"\r\n" | |
"pong"); | |
send(client_socket, response, strlen(response), 0); | |
} | |
// POST /create | |
else if (strcmp(method, "POST") == 0 && strcmp(path, "/create") == 0) { | |
char *body = get_body(request); | |
// Simple response - just acknowledge we got the JSON | |
sprintf(response, | |
"HTTP/1.1 200 OK\r\n" | |
"Content-Type: application/json\r\n" | |
"Content-Length: 23\r\n" | |
"\r\n" | |
"{\"status\": \"received\"}"); | |
send(client_socket, response, strlen(response), 0); | |
} | |
} | |
int main() { | |
int server_socket, client_socket; | |
struct sockaddr_in server_addr, client_addr; | |
socklen_t client_len = sizeof(client_addr); | |
char buffer[BUFFER_SIZE]; | |
// Create socket | |
server_socket = socket(AF_INET, SOCK_STREAM, 0); | |
if (server_socket < 0) { | |
perror("Socket creation failed"); | |
exit(1); | |
} | |
// Allow socket reuse | |
int opt = 1; | |
setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
// Setup address | |
server_addr.sin_family = AF_INET; | |
server_addr.sin_addr.s_addr = INADDR_ANY; | |
server_addr.sin_port = htons(PORT); | |
// Bind | |
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { | |
perror("Bind failed"); | |
exit(1); | |
} | |
// Listen | |
if (listen(server_socket, 5) < 0) { | |
perror("Listen failed"); | |
exit(1); | |
} | |
printf("Server running on port %d\n", PORT); | |
printf("GET /ping - Returns 'pong'\n"); | |
printf("POST /create - Accepts JSON with username and fruit\n\n"); | |
// Accept connections | |
while (1) { | |
client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_len); | |
if (client_socket < 0) { | |
perror("Accept failed"); | |
continue; | |
} | |
// Read request | |
memset(buffer, 0, BUFFER_SIZE); | |
read(client_socket, buffer, BUFFER_SIZE - 1); | |
// Handle request | |
handle_request(client_socket, buffer); | |
// Close connection | |
close(client_socket); | |
} | |
close(server_socket); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment