#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFFER_SIZE 1024
#define PORT 8080
// Function prototypes
void handle_request(int client_socket);
void send_response(int client_socket, char* message, int content_length, char* status_code);
int main() {
int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
socklen_t client_address_length = sizeof(client_address);
pid_t child_pid;
// Create server socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("socket creation failed");
exit(1);
}
// Set address and port number for the server
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr);
// Bind the server socket to the address and port
if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
perror("bind failed");
exit(1);
}
// Listen for incoming connections
if (listen(server_socket, 3) < 0) {
perror("listen failed");
exit(1);
}
printf("Server listening on port %d...\n", PORT);
while (1) {
// Accept a connection
client_socket = accept(server_socket, (struct sockaddr*)&client_address, &client_address_length);
if (client_socket < 0) {
perror("accept failed");
continue;
}
// Create a new process to handle the request
child_pid = fork();
if (child_pid == 0) {
// Child process handles the request
close(server_socket); // Close server socket in child
handle_request(client_socket);
close(client_socket); // Close client socket after handling
exit(0);
} else if (child_pid < 0) {
// Fork failed
perror("fork failed");
} else {
// Parent process closes client socket and continues listening
close(client_socket);
}
}
return 0;
}
void handle_request(int client_socket) {
char buffer[BUFFER_SIZE] = {0};
ssize_t bytes_received = recv(client_socket, buffer, BUFFER_SIZE, 0);
if (bytes_received <= 0) {
perror("recv failed");
send_response(client_socket, "Error receiving request", strlen("Error receiving request"), "500 Internal Server Error");
return;
}
buffer[bytes_received] = '\0'; // Null-terminate the buffer
printf("Received request: %s\n", buffer);
char* message = "Hello, World!";
int content_length = strlen(message);
char* status_code = "200 OK";
send_response(client_socket, message, content_length, status_code);
}
void send_response(int client_socket, char* message, int content_length, char* status_code) {
char response[BUFFER_SIZE] = {0};
// Construct HTTP response
strcat(response, "HTTP/1.1 ");
strcat(response, status_code);
strcat(response, "\r\nContent-Type: text/plain\r\n");
sprintf(response + strlen(response), "Content-Length: %d\r\n\r\n", content_length);
strcat(response, message);
ssize_t bytes_sent = send(client_socket, response, strlen(response), 0);
if (bytes_sent < 0) {
perror("send failed");
} else if (bytes_sent != strlen(response)) {
fprintf(stderr, "Error: Only sent %zd bytes of the response\n", bytes_sent);
}
}
Note: This server does not handle concurrent requests efficiently (using fork for each connection), and it lacks many features you'd expect from a production HTTP server. For real applications, consider using established HTTP server software or frameworks designed for your programming language of choice.
Concurrency with fork(): While using fork() works for simple cases, it's not efficient for handling a large number of concurrent requests. Each fork() creates a new process, which consumes system resources. For production servers, consider using threading (with pthread) or asynchronous I/O methods (like epoll or kqueue) for better performance and scalability.