Can you write an HTTP server in C++ without any framework?#
Without using any high-level frameworks, you can implement a simple HTTP request using the C++ standard library and some system calls. Below is a basic example that demonstrates how to create a simple HTTP client using header files like <iostream>
and <sys/socket.h>
from the C++ standard library to send requests to a server and receive responses.
Example: Simple HTTP Client#
This example will use raw socket programming to send an HTTP GET request and print the server's response.
#include <iostream>
#include <string>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
int main() {
// Server address and port
const char* server = "example.com";
const int port = 80;
// Create socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
std::cerr << "Error creating socket" << std::endl;
return 1;
}
// Server address structure
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
// Resolve server address
struct hostent* server_info = gethostbyname(server);
if (server_info == nullptr) {
std::cerr << "Error, no such host" << std::endl;
return 1;
}
// Copy server IP address to serv_addr structure
memcpy(&serv_addr.sin_addr.s_addr, server_info->h_addr, server_info->h_length);
// Connect to server
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
std::cerr << "Error connecting to server" << std::endl;
return 1;
}
// Send HTTP GET request
const char* request = "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n";
if (send(sockfd, request, strlen(request), 0) < 0) {
std::cerr << "Error sending request" << std::endl;
return 1;
}
// Receive server response
char buffer[4096];
int bytes_received = 0;
while ((bytes_received = recv(sockfd, buffer, sizeof(buffer) - 1, 0)) > 0) {
buffer[bytes_received] = '\0';
std::cout << buffer;
}
if (bytes_received < 0) {
std::cerr << "Error receiving response" << std::endl;
return 1;
}
// Close socket
close(sockfd);
return 0;
}
Code Explanation#
-
Create Socket:
- Use the
socket
function to create a TCP socket.
- Use the
-
Server Address Structure:
- Use the
sockaddr_in
structure to store the server's address information. - Use the
gethostbyname
function to resolve the server's domain name.
- Use the
-
Connect to Server:
- Use the
connect
function to connect to the server.
- Use the
-
Send HTTP Request:
- Construct a simple HTTP GET request string and use the
send
function to send the request.
- Construct a simple HTTP GET request string and use the
-
Receive Server Response:
- Use the
recv
function to receive the server's response and print it to standard output.
- Use the
-
Close Socket:
- Use the
close
function to close the socket.
- Use the
Compilation and Running#
Save the above code as http_client.cpp
, then compile and run using g++:
g++ -o http_client http_client.cpp
./http_client
Notes#
- This example is for demonstration purposes only; actual applications may need to handle more edge cases, such as timeouts, error handling, etc.
- To simplify the example, the separation of HTTP response headers and body is not handled here; actual applications may require more complex parsing logic.