HTTP请求协议格式如下:
HTTP响应协议格式如下:
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string>#pragma comment(lib, "ws2_32.lib")//std::string generateResponse(const std::string& request) {
// // 在此处根据实际需求生成相应的HTTP响应
// std::string response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
// response += "<html><body><h1>Hello, World!</h1></body></html>";
// return response;
//}std::string generateResponse(const std::string& request) {// 在此处根据实际需求生成相应的HTTP响应std::string response = "HTTP/1.1 200 OK\r\n";response += "Content-Type: text/html\r\n";response += "Access-Control-Allow-Origin: *\r\n";//response += "Access-Control-Allow-Origin: http://localhost:8080\r\n"; // 允许http://localhost:8080的请求response += "Access-Control-Allow-Methods: GET, POST\r\n"; // 允许的请求方法response += "Access-Control-Allow-Headers: Content-Type\r\n"; // 允许的请求头response += "\r\n";response += "<html><body><h1>Hello, World!</h1></body></html>";return response;
}int main() {// 初始化WinsockWSADATA wsaData;if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {std::cerr << "Failed to initialize Winsock." << std::endl;return -1;}// 创建监听套接字SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);if (serverSocket == INVALID_SOCKET) {std::cerr << "Failed to create server socket." << std::endl;WSACleanup();return -1;}// 绑定地址和端口sockaddr_in serverAddress;serverAddress.sin_family = AF_INET; // IPv4serverAddress.sin_addr.s_addr = INADDR_ANY; // 使用任意可用地址serverAddress.sin_port = htons(8080); // 监听端口8080if (bind(serverSocket, (sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {std::cerr << "Bind failed with error: " << WSAGetLastError() << std::endl;closesocket(serverSocket);WSACleanup();return -1;}// 开始监听if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {std::cerr << "Listen failed with error: " << WSAGetLastError() << std::endl;closesocket(serverSocket);WSACleanup();return -1;}std::cout << "Server started. Waiting for connections..." << std::endl;while (true) {// 等待客户端连接sockaddr_in clientAddress;int clientAddressSize = sizeof(clientAddress);SOCKET clientSocket = accept(serverSocket, (sockaddr*)&clientAddress, &clientAddressSize);if (clientSocket == INVALID_SOCKET) {std::cerr << "Accept failed with error: " << WSAGetLastError() << std::endl;closesocket(serverSocket);WSACleanup();return -1;}std::cout << "Client connected." << std::endl;// 接收和发送数据const int bufferSize = 4096;char requestBuffer[bufferSize];memset(requestBuffer, 0, sizeof(requestBuffer));if (recv(clientSocket, requestBuffer, sizeof(requestBuffer), 0) == SOCKET_ERROR) {std::cerr << "Receive failed with error: " << WSAGetLastError() << std::endl;closesocket(clientSocket);closesocket(serverSocket);WSACleanup();return -1;}std::string request(requestBuffer);// 解析HTTP请求,获取请求路径等信息std::string requestPath;size_t firstSpace = request.find(' ');size_t secondSpace = request.find(' ', firstSpace + 1);if (firstSpace != std::string::npos && secondSpace != std::string::npos) {requestPath = request.substr(firstSpace + 1, secondSpace - firstSpace - 1);}std::cout << "Request: " << requestPath << std::endl;// 生成HTTP响应std::string response = generateResponse(requestPath);// 发送HTTP响应if (send(clientSocket, response.c_str(), response.size(), 0) == SOCKET_ERROR) {std::cerr << "Send failed with error: " << WSAGetLastError() << std::endl;closesocket(clientSocket);closesocket(serverSocket);WSACleanup();return -1;}// 关闭客户端连接std::cout << "Client disconnected." << std::endl;closesocket(clientSocket);}// 清理Winsockclosesocket(serverSocket);WSACleanup();return 0;
}
test(){fetch("http://127.0.0.1:8080").then((response: Response) => {return response.text()}).then((value) => {console.log(value);console.log("dsfasd");})}