Hello friend, I am Tapas. In today’s blog, I will talk about How to Create Web Server in C using any programming language.
Table of Contents
ToggleRequired Libraries
#include
#include
#include
#include
#include
#include
#include
How to Create Web Server in C
#include
#include
#include
#include
#include
#include
#include
#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"
"Hello from C Web Server!
";
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)
-
AF_INET: Its full name is Address Family Internet. It says that I will use my IPv4 address (eg:- 192.168.1.10)
. -
SOCK_STREAM: This says that we want a TCP stream connection, which will come in the form of a continuous stream instead of eight data packets.
. - 0: This is the protocol number. 0 means the system will automatically choose the default protocol from that address, that address family + socket type, or according to itself.
2. Socket Configuration
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
-
INADDR_ANY: This means that the server can accept connections from all network interfaces of the machine (localhost, LAN IP).
. - htons(PORT): This stands for host-to-network. It converts the port number into network bytes.
3. Binding and Listening
bind(server_fd, (struct sockaddr *)&address, sizeof(address))
listen(server_fd, 3)
-
bind(): This function associates the socket with the specified IP address and port number.
. - listen(): After bind() is successful, the socket will now wait for incoming connections by calling listen().
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.
