How to Create Web Server in C

Hello friend, I am Tapas. In today’s blog, I will talk about How to Create Web Server in C using any programming language.

				
					#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
				
			

How to Create Web Server in C

				
					#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 8080
#define BUFFER_SIZE 1024

void handle_client(int client_socket) {
    char buffer[BUFFER_SIZE];
    char response[] = "HTTP/1.1 200 OK\r\n"
                     "Content-Type: text/html\r\n\r\n"
                     "<html><body><h1><span class="ez-toc-section" id="Hello_from_C_Web_Server"></span>Hello from C Web Server!<span class="ez-toc-section-end"></span></h1></body></html>";
    
    read(client_socket, buffer, BUFFER_SIZE);
    printf("Received request:\n%s\n", buffer);
    
    write(client_socket, response, strlen(response));
    close(client_socket);
}

int main() {
    int server_fd, client_socket;
    struct sockaddr_in address;
    int addrlen = sizeof(address);
    
    // Create socket
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        perror("Socket failed");
        exit(EXIT_FAILURE);
    }
    
    // Configure socket
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(PORT);
    
    // Bind socket to port
    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
        perror("Bind failed");
        exit(EXIT_FAILURE);
    }
    
    // Listen for connections
    if (listen(server_fd, 3) < 0) {
        perror("Listen failed");
        exit(EXIT_FAILURE);
    }
    
    printf("Server running on port %d\n", PORT);
    
    // Main server loop
    while (1) {
        if ((client_socket = accept(server_fd, (struct sockaddr *)&address, 
                                   (socklen_t*)&addrlen)) < 0) {
            perror("Accept failed");
            continue;
        }
        
        printf("Connection accepted from %s:%d\n", 
               inet_ntoa(address.sin_addr), ntohs(address.sin_port));
        
        handle_client(client_socket);
    }
    
    return 0;
}
				
			

Explain Code

1. Socket Creation

				
					socket(AF_INET, SOCK_STREAM, 0)
				
			

2. Socket Configuration

				
					address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
				
			

3. Binding and Listening

				
					bind(server_fd, (struct sockaddr *)&address, sizeof(address))
listen(server_fd, 3)
				
			

4. Client Handling

				
					void handle_client(int client_socket) {
// Prepares HTTP response
// Reads client request
// Sends response
}
				
			

Compile and run

				
					gcc -o webserver webserver.c
./webserver
				
			

Test

Go to browser: http://localhost:8080

Output

				
					Server running on port 8080
Connection accepted from 127.0.0.1:52345
Received request:
GET / HTTP/1.1
Host: localhost:8080
...
				
			

Conculsion

This basic web server will help you understand the basics of network programming. You can later add features like serving static files, handling multiclients, etc. Let me know how you liked today’s project in the comments and if you have any doubts, comment or message me on my WhatsApp.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *