c++编程实现网络http服务器
时间: 2023-10-03 12:03:06 浏览: 62
实现一个简单的 HTTP 服务器可以分为以下几个步骤:
1. 创建一个 socket,绑定到指定的 IP 地址和端口号;
2. 监听 socket,等待客户端连接;
3. 接收客户端请求;
4. 处理客户端请求,生成响应数据;
5. 发送响应数据给客户端;
6. 关闭连接。
下面是一个简单的 C 语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8080
#define BUFFER_SIZE 1024
void handle_request(int client_socket) {
char buffer[BUFFER_SIZE];
ssize_t bytes_read = recv(client_socket, buffer, BUFFER_SIZE, 0);
if (bytes_read < 0) {
perror("recv failed");
return;
}
buffer[bytes_read] = '\0';
printf("Received request:\n%s\n", buffer);
char response[] = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello, world!";
ssize_t bytes_written = send(client_socket, response, strlen(response), 0);
if (bytes_written < 0) {
perror("send failed");
return;
}
printf("Sent response:\n%s\n", response);
}
int main() {
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_address = {0};
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(PORT);
if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_socket, 5) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d...\n", PORT);
while (1) {
struct sockaddr_in client_address = {0};
socklen_t client_address_size = sizeof(client_address);
int client_socket = accept(server_socket, (struct sockaddr *)&client_address, &client_address_size);
if (client_socket < 0) {
perror("accept failed");
continue;
}
handle_request(client_socket);
close(client_socket);
}
close(server_socket);
return 0;
}
```
这个代码实现了一个简单的 HTTP 服务器,它监听在 8080 端口上,收到客户端请求后会返回一个固定的响应。你可以在浏览器中输入 `http://localhost:8080` 来访问这个服务器。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)