Table of Contents
Learning network programming is essential for understanding how the internet works. Building a simple HTTP server in C is a great way to grasp the basics of network communication, socket programming, and web protocols. This article guides you through creating a basic HTTP server to serve static pages, which is a foundational skill for more complex web server development.
Prerequisites
- Basic knowledge of C programming
- Understanding of network protocols, especially TCP/IP
- Development environment with a C compiler (like gcc)
- Familiarity with command-line tools
Basic Structure of the HTTP Server
The server will perform these main tasks:
- Create a socket and bind it to a port
- Listen for incoming connections
- Accept client requests
- Read HTTP requests
- Send back HTTP responses
- Close connections
Sample Code Overview
Below is a simplified example of a basic HTTP server in C. It handles one request at a time and serves a fixed HTML page.
Remember to compile with gcc -o simple_http_server server.c and run with appropriate permissions.
Here’s the complete code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addr_len = sizeof(address);
char buffer[1024] = {0};
const char *http_response =
"HTTP/1.1 200 OK\\r\\n"
"Content-Type: text/html\\r\\n"
"Content-Length: 46\\r\\n"
"\\r\\n"
"Hello, World!
";
// Create socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}
// Bind the socket to the port
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_fd, 3) < 0) {
perror("Listen");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Server listening on port %d\n", PORT);
while (1) {
// Accept a new connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addr_len)) < 0) {
perror("Accept");
continue;
}
// Read request
read(new_socket, buffer, 1024);
printf("Received request:\n%s\n", buffer);
// Send response
write(new_socket, http_response, strlen(http_response));
close(new_socket);
}
close(server_fd);
return 0;
}
Running and Testing the Server
Compile the server code:
gcc -o simple_http_server server.c
Run the server:
./simple_http_server
Open a web browser and navigate to http://localhost:8080. You should see the "Hello, World!" message displayed.
Conclusion
This simple HTTP server demonstrates the fundamental concepts of network programming in C. You can expand it by adding support for multiple concurrent connections, parsing HTTP headers, or serving different pages based on the request. Building such projects enhances your understanding of how web servers work behind the scenes.