建立socket连接之后,编写c++函数接收客户端发来的post请求
时间: 2023-08-16 15:05:47 浏览: 159
在C++中,可以使用socket和HTTP协议库来实现接收客户端发来的POST请求。
首先,创建一个socket连接并监听客户端请求:
```cpp
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
// 创建socket
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
// 绑定IP地址和端口号
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080); // 端口号
server_addr.sin_addr.s_addr = INADDR_ANY;
bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
// 监听客户端请求
listen(server_fd, 5);
// 接收客户端请求
struct sockaddr_in client_addr;
socklen_t client_addrlen = sizeof(client_addr);
int client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_addrlen);
}
```
接下来,读取客户端发来的POST请求,并解析请求头和请求体:
```cpp
#include <sstream>
// 读取数据
std::stringstream ss;
char buffer[1024];
int len;
while ((len = recv(client_fd, buffer, 1024, 0)) > 0) {
ss << buffer;
}
std::string request = ss.str();
std::string request_header = request.substr(0, request.find("\r\n\r\n")); //请求头
std::string request_body = request.substr(request.find("\r\n\r\n") + 4); //请求体
```
根据HTTP协议规定,POST请求的请求体中通常包含表单数据,需要对其进行解析。这里可以使用第三方库来实现。
例如,使用cpp-httplib库:
```cpp
#include "httplib.h"
// 解析请求体
httplib::Params params;
httplib::parse_multipart_formdata(request_body, "boundary", params);
```
最后,返回响应给客户端:
```cpp
// 返回响应
std::string response_body = "Hello World!";
std::stringstream response;
response << "HTTP/1.1 200 OK\r\n";
response << "Content-Type: text/html; charset=UTF-8\r\n";
response << "Content-Length: " << response_body.length() << "\r\n";
response << "\r\n";
response << response_body;
send(client_fd, response.str().c_str(), response.str().length(), 0);
```
完整代码如下:
```cpp
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "httplib.h"
int main() {
// 创建socket
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
// 绑定IP地址和端口号
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080); // 端口号
server_addr.sin_addr.s_addr = INADDR_ANY;
bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
// 监听客户端请求
listen(server_fd, 5);
// 接收客户端请求
struct sockaddr_in client_addr;
socklen_t client_addrlen = sizeof(client_addr);
int client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_addrlen);
// 读取数据
std::stringstream ss;
char buffer[1024];
int len;
while ((len = recv(client_fd, buffer, 1024, 0)) > 0) {
ss << buffer;
}
std::string request = ss.str();
std::string request_header = request.substr(0, request.find("\r\n\r\n")); //请求头
std::string request_body = request.substr(request.find("\r\n\r\n") + 4); //请求体
// 解析请求体
httplib::Params params;
httplib::parse_multipart_formdata(request_body, "boundary", params);
for (const auto& param : params) {
std::cout << param.first << " = " << param.second << std::endl;
}
// 返回响应
std::string response_body = "Hello World!";
std::stringstream response;
response << "HTTP/1.1 200 OK\r\n";
response << "Content-Type: text/html; charset=UTF-8\r\n";
response << "Content-Length: " << response_body.length() << "\r\n";
response << "\r\n";
response << response_body;
send(client_fd, response.str().c_str(), response.str().length(), 0);
// 关闭socket连接
close(client_fd);
close(server_fd);
return 0;
}
```
阅读全文