中文久久,精品伦精品一区二区三区视频,美国AV一区二区三区,国产免费小视频

意見箱
恒創(chuàng)運(yùn)營部門將仔細(xì)參閱您的意見和建議,必要時(shí)將通過預(yù)留郵箱與您保持聯(lián)絡(luò)。感謝您的支持!
意見/建議
提交建議

c語言怎么寫web服務(wù)器

來源:佚名 編輯:佚名
2024-03-30 23:01:57

要使用C語言編寫一個(gè)Web服務(wù)器,你需要了解HTTP協(xié)議、套接字編程以及多線程等技術(shù),下面是一個(gè)簡單的C語言Web服務(wù)器的實(shí)現(xiàn)過程:

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、創(chuàng)建套接字并綁定地址:

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、創(chuàng)建線程處理客戶端連接:

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.
本網(wǎng)站發(fā)布或轉(zhuǎn)載的文章均來自網(wǎng)絡(luò),其原創(chuàng)性以及文中表達(dá)的觀點(diǎn)和判斷不代表本網(wǎng)站。
上一篇: 墨爾本服務(wù)器租用價(jià)格受哪些因素影響? 下一篇: 云服務(wù)器重置密碼會(huì)掉數(shù)據(jù)嗎
相關(guān)文章
查看更多