自学内容网 自学内容网

C++ windows HTTP 服务器demo

C++ windows HTTP 服务器 ,用来验证MES的post get

#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string>

// 引入静态链接库
#pragma comment(lib, "ws2_32.lib")

const int DEFAULT_PORT = 8889;

// 处理GET请求的函数,这里简单返回固定内容示例
std::string handleGetRequest(const std::string& request) {
    std::string response = "HTTP/1.1 200 OK\r\n";
    response += "Content-Type: text/html; charset=UTF-8\r\n";
    response += "\r\n";
    response += "<html><body><h1>Hello from GET!</h1></body></html>";
    return response;
}

// 处理POST请求的函数,这里简单返回固定内容示例,实际中可根据POST数据做处理
std::string handlePostRequest(const std::string& request) {
    std::string response = "HTTP/1.1 200 OK\r\n";
    response += "Content-Type: text/html; charset=UTF-8\r\n";
    response += "\r\n";
    response += "<html><body><h1>Hello from POST!</h1></body></html>";
    return response;
}

int main() {
    // 初始化WinSock
    WSADATA wsaData;
    int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (result != 0) {
        std::cerr << "WSAStartup failed: " << result << std::endl;
        return 1;
    }

    // 创建套接字
    SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (listenSocket == INVALID_SOCKET) {
        std::cerr << "Socket creation failed: " << WSAGetLastError() << std::endl;
        WSACleanup();
        return 1;
    }

    // 绑定地址和端口
    sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = INADDR_ANY;
    serverAddr.sin_port = htons(DEFAULT_PORT);
    result = bind(listenSocket, (sockaddr*)&serverAddr, sizeof(serverAddr));
    if (result == SOCKET_ERROR) {
        std::cerr << "Bind failed: " << WSAGetLastError() << std::endl;
        closesocket(listenSocket);
        WSACleanup();
        return 1;
    }

    // 监听端口
    result = listen(listenSocket, SOMAXCONN);
    if (result == SOCKET_ERROR) {
        std::cerr << "Listen failed: " << WSAGetLastError() << std::endl;
        closesocket(listenSocket);
        WSACleanup();
        return 1;
    }

    std::cout << "Server is listening on port " << DEFAULT_PORT << std::endl;

    while (true) {
        // 接受客户端连接
        SOCKET clientSocket = accept(listenSocket, NULL, NULL);
        if (clientSocket == INVALID_SOCKET) {
            std::cerr << "Accept failed: " << WSAGetLastError() << std::endl;
            continue;
        }

        char buffer[1024];
        int bytesReceived = recv(clientSocket, buffer, sizeof(buffer), 0);
        if (bytesReceived > 0) {
            buffer[bytesReceived] = '\0';
            std::string request(buffer);
            std::string response;

            // 判断请求方法是GET还是POST
            if (request.find("GET") == 0) {
                response = handleGetRequest(request);
            }
            else if (request.find("POST") == 0) {
                response = handlePostRequest(request);
            }
            else {
                // 对于不支持的方法返回405 Method Not Allowed
                response = "HTTP/1.1 405 Method Not Allowed\r\n";
                response += "Content-Type: text/html; charset=UTF-8\r\n";
                response += "\r\n";
                response += "<html><body><h1>405 Method Not Allowed</h1></body></html>";
            }

            // 发送响应给客户端
            send(clientSocket, response.c_str(), response.length(), 0);
        }

        // 关闭客户端套接字
        closesocket(clientSocket);
    }

    // 关闭监听套接字,清理WinSock环境
    closesocket(listenSocket);
    WSACleanup();
    return 0;
}

原文地址:https://blog.csdn.net/doubleintfloat/article/details/144382363

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!