C++ windows HTTP 服务器demo

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

cpp 复制代码
#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;
}
相关推荐
网络抓包与爬虫1 小时前
flutter WEB端启动优化(加载速度,加载动画)
websocket·网络协议·tcp/ip·http·网络安全·https·udp
网络抓包与爬虫1 小时前
从头开发一个Flutter插件(二)高德地图定位插件
websocket·网络协议·tcp/ip·http·网络安全·https·udp
色的归属感2 小时前
一款功能强大的手机使用情况监控工具
websocket·网络协议·tcp/ip·http·网络安全·https·udp
iOS技术狂热者2 小时前
【Android开发基础】手机传感器信息的获取
websocket·网络协议·tcp/ip·http·网络安全·https·udp
双叶8366 小时前
(C语言)虚数运算(结构体教程)(指针解法)(C语言教程)
c语言·开发语言·数据结构·c++·算法·microsoft
@hdd7 小时前
Qt实现HTTP GET/POST/PUT/DELETE请求
qt·http
牵牛老人9 小时前
C++设计模式-责任链模式:从基本介绍,内部原理、应用场景、使用方法,常见问题和解决方案进行深度解析
c++·设计模式·责任链模式
swift开发pk OC开发9 小时前
如何轻松查看安卓手机内存,让手机更流畅
websocket·网络协议·tcp/ip·http·网络安全·https·udp
序属秋秋秋9 小时前
算法基础_基础算法【高精度 + 前缀和 + 差分 + 双指针】
c语言·c++·学习·算法
KeithTsui10 小时前
GCC RISCV 后端 -- 控制流(Control Flow)的一些理解
linux·c语言·开发语言·c++·算法