使用标准C语言实现Web服务器,完成基础的tcp连接,支持基础的client与其连接使用fork()来支持并发访问服务器简单的http访问,支持静态页面访问 支持静态页面访问,需要一定的报错机制,如404页面的建立。 (1)socket实现简单Http服务器,完成html的解析; (2)运行该服务器可以通过浏览器访问服务器目录下的 Html文件、jpg图片、css文件的载入。完成初步的Http服务器功能。C语言Window
时间: 2024-02-03 22:14:15 浏览: 72
c语言多进程tcp服务器示例
实现Web服务器需要以下步骤:
1. 创建socket并绑定端口
2. 监听端口,等待客户端连接
3. 接收客户端请求并解析HTTP请求
4. 处理请求并返回响应
5. 关闭连接
以下是一个简单的C语言Web服务器示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8080
#define BUFFER_SIZE 1024
void handle_request(int client_socket) {
char buffer[BUFFER_SIZE];
ssize_t buffer_size;
buffer_size = recv(client_socket, buffer, BUFFER_SIZE, 0);
if (buffer_size < 0) {
perror("Error receiving data from client");
return;
}
// Parse HTTP request
char method[16], path[128], protocol[16];
sscanf(buffer, "%s %s %s", method, path, protocol);
// Open requested file
char *file_path = path + 1;
if (strcmp(file_path, "") == 0) {
file_path = "index.html";
}
FILE *file = fopen(file_path, "rb");
if (file == NULL) {
// Return 404 error
char *error_message = "HTTP/1.1 404 Not Found\r\n\r\n";
send(client_socket, error_message, strlen(error_message), 0);
return;
}
// Send HTTP response
char *response_header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
send(client_socket, response_header, strlen(response_header), 0);
while ((buffer_size = fread(buffer, 1, BUFFER_SIZE, file)) > 0) {
send(client_socket, buffer, buffer_size, 0);
}
fclose(file);
}
int main() {
int server_socket, client_socket, opt = 1;
struct sockaddr_in address;
int address_size = sizeof(address);
// Create server socket
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Error creating server socket");
exit(EXIT_FAILURE);
}
// Set socket options
if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("Error setting socket options");
exit(EXIT_FAILURE);
}
// Bind server socket to port
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_socket, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Error binding server socket to port");
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_socket, 3) < 0) {
perror("Error listening for incoming connections");
exit(EXIT_FAILURE);
}
printf("Server running on port %d\n", PORT);
// Handle incoming connections
while (1) {
if ((client_socket = accept(server_socket, (struct sockaddr *)&address, (socklen_t *)&address_size)) < 0) {
perror("Error accepting incoming connection");
exit(EXIT_FAILURE);
}
printf("Connection accepted from %s:%d\n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
if (fork() == 0) {
// Child process handles request
handle_request(client_socket);
close(client_socket);
exit(EXIT_SUCCESS);
} else {
// Parent process continues listening
close(client_socket);
}
}
return 0;
}
```
该代码实现了一个简单的Web服务器,可以处理GET请求并返回静态HTML文件。要使用该服务器,只需将HTML文件放在服务器目录下,并通过浏览器访问服务器地址即可。例如,如果服务器运行在本地端口8080上,可以通过浏览器访问http://localhost:8080/index.html来查看index.html文件。
阅读全文