用c语言开发web服务器并加载html网页,html网页中显示图片
时间: 2024-04-26 08:27:06 浏览: 151
要用C语言开发Web服务器并加载HTML网页,您可以使用Socket编程,具体步骤如下:
1. 创建Socket,绑定IP地址和端口号,然后进行监听。
2. 接收客户端请求,解析HTTP请求报文,获取请求的资源路径。
3. 根据请求的资源路径,读取对应的HTML文件,并将其发送给客户端。
4. 如果HTML文件中包含图片等其他资源,也需要将其读取并发送给客户端。
以下是一个简单的示例代码,用于加载HTML网页并显示图片(假设图片路径为"/img/test.jpg"):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 80
#define MAX_CLIENTS 5
#define BUFFER_SIZE 1024
void handle_request(int client_socket);
int main(int argc, char *argv[]) {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len;
// 创建Socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if(server_socket == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// 设置Socket选项
int opt = 1;
if(setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
perror("Error setting socket options");
exit(EXIT_FAILURE);
}
// 绑定IP地址和端口号
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if(bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Error binding socket");
exit(EXIT_FAILURE);
}
// 监听Socket
if(listen(server_socket, MAX_CLIENTS) == -1) {
perror("Error listening on socket");
exit(EXIT_FAILURE);
}
printf("Server started on port %d\n", PORT);
// 无限循环,等待客户端请求
while(1) {
client_len = sizeof(client_addr);
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_len);
if(client_socket == -1) {
perror("Error accepting client connection");
exit(EXIT_FAILURE);
}
printf("Client connected: %s\n", inet_ntoa(client_addr.sin_addr));
// 处理客户端请求
handle_request(client_socket);
// 关闭Socket
close(client_socket);
}
return 0;
}
void handle_request(int client_socket) {
char buffer[BUFFER_SIZE];
char *response;
FILE *file;
long file_size;
// 读取客户端请求
if(recv(client_socket, buffer, BUFFER_SIZE, 0) == -1) {
perror("Error receiving client request");
exit(EXIT_FAILURE);
}
// 解析HTTP请求报文,获取请求的资源路径
char *path = strtok(buffer, " ");
path = strtok(NULL, " ");
printf("Requested path: %s\n", path);
// 读取HTML文件
if(strcmp(path, "/") == 0) {
file = fopen("index.html", "rb");
} else {
file = fopen(path + 1, "rb");
}
if(file == NULL) {
response = "HTTP/1.1 404 Not Found\r\n\r\n";
send(client_socket, response, strlen(response), 0);
return;
}
// 获取HTML文件大小
fseek(file, 0, SEEK_END);
file_size = ftell(file);
fseek(file, 0, SEEK_SET);
// 分配内存,保存HTML文件内容
response = (char *)malloc(file_size);
fread(response, 1, file_size, file);
// 关闭文件
fclose(file);
// 发送HTTP响应报文和HTML文件内容
char headers[BUFFER_SIZE];
sprintf(headers, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %ld\r\n\r\n", file_size);
send(client_socket, headers, strlen(headers), 0);
send(client_socket, response, file_size, 0);
// 显示图片
if(strcmp(path, "/img/test.jpg") == 0) {
file = fopen(path + 1, "rb");
if(file == NULL) {
perror("Error loading image file");
exit(EXIT_FAILURE);
}
fseek(file, 0, SEEK_END);
file_size = ftell(file);
fseek(file, 0, SEEK_SET);
response = (char *)malloc(file_size);
fread(response, 1, file_size, file);
fclose(file);
sprintf(headers, "HTTP/1.1 200 OK\r\nContent-Type: image/jpeg\r\nContent-Length: %ld\r\n\r\n", file_size);
send(client_socket, headers, strlen(headers), 0);
send(client_socket, response, file_size, 0);
}
// 释放内存
free(response);
}
```
在运行程序后,您可以在浏览器中输入"http://localhost",就可以看到加载的HTML网页,并显示图片。当然您需要将图片放在正确的路径下,并修改代码中的图片路径。
阅读全文