标准C语言实现简单Web服务器完成基础的tcp连接,并且支持基础的client与其连接使用fork()来支持并发访问服务器简单的http访问,支持静态页面访问 支持静态页面访问,需要一定的报错机制,如404页面的建立。 (1)socket实现简单Http服务器,完成html的解析; (2)运行该服务器可以通过浏览器访问服务器目录下的 Html文件、jpg图片、css文件的载入。完成初步的Http服务器功能。
时间: 2024-03-05 20:53:25 浏览: 69
以下是一个简单的基于C语言实现的Web服务器,支持静态页面访问,并且具有一定的报错机制(404页面):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SERVER_PORT 8080
#define BUFFER_SIZE 1024
char *get_file_extension(char *path) {
char *ext = strrchr(path, '.');
if (ext == NULL) {
return "";
}
return ext + 1;
}
char *get_mime_type(char *ext) {
if (strcmp(ext, "html") == 0 || strcmp(ext, "htm") == 0) {
return "text/html";
} else if (strcmp(ext, "css") == 0) {
return "text/css";
} else if (strcmp(ext, "jpg") == 0 || strcmp(ext, "jpeg") == 0) {
return "image/jpeg";
} else if (strcmp(ext, "png") == 0) {
return "image/png";
} else if (strcmp(ext, "gif") == 0) {
return "image/gif";
} else {
return "application/octet-stream";
}
}
void handle_client(int client_sock) {
char buffer[BUFFER_SIZE];
recv(client_sock, buffer, sizeof(buffer), 0);
char *path = strtok(buffer, " ");
path = strtok(NULL, " ");
if (path == NULL) {
path = "/";
}
char file_path[1024];
sprintf(file_path, ".%s", path);
int fd = open(file_path, O_RDONLY);
if (fd == -1) {
char *error_page = "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1></body></html>";
char response_header[1024];
sprintf(response_header, "HTTP/1.1 404 Not Found\r\nContent-Length: %d\r\nContent-Type: text/html\r\n\r\n", strlen(error_page));
send(client_sock, response_header, strlen(response_header), 0);
send(client_sock, error_page, strlen(error_page), 0);
} else {
struct stat st;
fstat(fd, &st);
char *ext = get_file_extension(file_path);
char *mime_type = get_mime_type(ext);
char response_header[1024];
sprintf(response_header, "HTTP/1.1 200 OK\r\nContent-Length: %ld\r\nContent-Type: %s\r\n\r\n", st.st_size, mime_type);
send(client_sock, response_header, strlen(response_header), 0);
char file_buffer[BUFFER_SIZE];
int bytes_read;
while ((bytes_read = read(fd, file_buffer, BUFFER_SIZE)) > 0) {
send(client_sock, file_buffer, bytes_read, 0);
}
close(fd);
}
close(client_sock);
}
int main() {
int server_sock, client_sock;
struct sockaddr_in server_addr, client_addr;
int client_addr_len = sizeof(client_addr);
server_sock = socket(AF_INET, SOCK_STREAM, 0);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
listen(server_sock, 5);
printf("Web server listening on port %d...\n", SERVER_PORT);
while (1) {
client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &client_addr_len);
if (fork() == 0) {
handle_client(client_sock);
exit(0);
}
close(client_sock);
}
return 0;
}
```
该代码中,handle_client函数处理客户端的请求,如果请求的文件存在,则返回该文件的内容,否则返回一个简单的404页面。
在main函数中,使用fork函数创建子进程来处理客户端的请求,从而支持并发访问。
阅读全文