要使用C语言编写一个Web服务器,你需要了HTTP协议、套接字编程以及多线程等技术,下面是一个简单的C语言Web服务器的实现过程:

c语言怎么写web服务器c语言怎么写web服务器(图片来源网络,侵删)

1、需要包含一些必要的头文件:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>

2、定义常量和全局变量:

#define PORT 8080
#define BUFFER_SIZE 4096
#define MAX_CONNECTIONS 5
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
pthread_t thread_id;
int client_count = 0;

3、创建套接字并绑定地址

server_fd = 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 = INADDR_ANY;
server_addr.sin_port = htons(PORT);
bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
listen(server_fd, MAX_CONNECTIONS);

4、创建线程处理客户端连接:

void *handle_client(void *arg) {
    int client_socket = *((int *)arg);
    char buffer[BUFFER_SIZE];
    char oldPath[1024] = {0};
    char newPath[1024] = {0};
    ssize_t bytesRead;
    char fileName[1024] = {0};
    char fileType[1024] = {0};
    char fullPath[1024] = {0};
    char response[BUFFER_SIZE] = "HTTP/1.1 200 OKr
ContentType: text/htmlr
r
";
    char errorResponse[BUFFER_SIZE] = "HTTP/1.1 404 Not Foundr
ContentType: text/htmlr
r
";
    char defaultPage[] = "<!DOCTYPE html><html><head><title>Welcome</title></head><body><h1>Welcome to my web server!</h1></body></html>";
    char indexPage[] = "<!DOCTYPE html><html><head><title>Index</title></head><body><h1>Index page</h1></body></html>";
    char aboutPage[] = "<!DOCTYPE html><html><head><title>About</title></head><body><h1>About page</h1></body></html>";
    char notFoundPage[] = "<!DOCTYPE html><html><head><title>Not Found</title></head><body><h1>404 Not Found</h1></body></html>";
    sprintf(fullPath, "%s/%s", oldPath, fileName); // Full path of the requested file
    if (access(fullPath, F_OK) == 0) { // File exists, serve it
        bytesRead = read(client_socket, buffer, sizeof(buffer) 1); // Read request from client
        if (strncmp(buffer, "GET", 3) == 0) { // If request is a GET request
            sscanf(buffer, "GET %s HTTP/1.1", fileName); // Get requested file name from request line
            if (strcmp(fileName, "/") == 0) { // If requested file is root directory index page
                strcpy(response, indexPage); // Set response as index page HTML content
            } else if (strcmp(fileName, "/about") == 0) { // If requested file is about page
                strcpy(response, aboutPage); // Set response as about page HTML content
            } else { // If requested file is other than root directory or about page, serve it as static file
                if (strstr(fileName, ".")) { // If requested file has extension, set response as error response (404 Not Found) and send default page instead of requested file to avoid serving potentially harmful files with extensions like .php, .js, etc.
                    strcpy(response, errorResponse); // Set response as error response (404 Not Found) HTML content
                    send(client_socket, defaultPage, strlen(defaultPage), 0); // Send default page to client instead of requested file to avoid serving potentially harmful files with extensions like .php, .js, etc.
                } else { // If requested file has no extension, set response as error response (404 Not Found) and send default page instead of requested file to avoid serving potentially harmful files with extensions like .php, .js, etc. and also to avoid serving directory listings which can reveal sensitive information about the server's files and directories structure and contents if directory listing is enabled on the server for some reason like by mistake or intentionally for testing purposes or something else.
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。