如何接收猫鼬发送的数据?
How can I receive the sent data in mongoose?
我正在使用mongoose.c,并且我编写了一个程序,以便接收POST请求。场景是猫鼬应该接收 JSON 并将其保存到文件config.conf
:
为了管理它,我编写了以下代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include "Mongoose/Mongoose.h"
static void saveToFile(std::string fileName, std::string text)
{
FILE *file;
file = fopen(fileName.c_str(), "wb");
fwrite(text.c_str(), sizeof(text[0]), sizeof(text)/sizeof(text[0]), file);
fclose(file);
}
static void ev_handler_to_Receive_file(struct mg_connection *nc, int ev, void *ev_data)
{
char buff[65536];
if (ev == MG_EV_HTTP_REQUEST)
{
struct http_message *hm = (struct http_message *) ev_data;
std::string uri = hm->uri.p;
// We have received an HTTP request. Parsed request is contained in `hm`.
// Send HTTP reply to the client which shows full original request.
if (strncmp(hm->method.p, "POST", hm->method.len) == 0)
{
std::cout << "POST DETECTED" << std::endl;
if (std::string::npos != uri.find("/config"))
{
std::cout << "#config# DETECTED" << std::endl;
//Read post data
memcpy(buff, hm->body.p, hm->body.len);
std::string data (buff);
std::cout << buff << std::endl;
saveToFile("config.conf", buff);
}
}
mg_send_head(nc, 200, hm->message.len, "Content-Type: text/plain");
mg_printf(nc, "%.*s", (int)hm->message.len, hm->message.p);
}
}
void initializeServer()
{
int numberOfPolls = 1;
struct mg_mgr mongooseEventManager;
struct mg_connection *mongooseConnection;
// Start Web Server
mg_mgr_init(&mongooseEventManager, NULL);
mongooseConnection = mg_bind(&mongooseEventManager, "8000", ev_handler_to_Receive_file);
mg_set_protocol_http_websocket(mongooseConnection);
while(true)
{
printf("POLL[%i]n", numberOfPolls);
mg_mgr_poll(&mongooseEventManager, 500);
numberOfPolls++;
}
mg_mgr_free(&mongooseEventManager);
}
int main()
{
initializeServer();
return 0;
}
该程序适用于短 JSON 文件,但如果 JSON 文件大于大约 20 个字符,我会丢失其余的 JSON 文本。
例如,如果我发送以下 JSON 文件,一切正常:
{
TEST:true
}
将创建一个 config.conf 文件并包含 JSON 文本。
另一方面,如果我发送例如以下 JSON 文件:
{
TEST:true,
TEST2:false,
TEST3:false
}
生成的文件包含以下文本:
{
TEST:true,
TEST2:false,
如果有人知道如何解决这个问题,我将不胜感激任何信息。
我认为您的问题是由于将字符*复制到std::string以及我们将内容写入文件的方式。
memcpy(buff, hm->body.p, hm->body.len);
std::string data (buff);
std::cout << buff << std::endl;
saveToFile("config.conf", buff);
您可以使用 std::string 构造函数转换此指针,如下所示
std::cout << "#config# DETECTED" << std::endl;
std::string data(hm->body.p, hm->body.len);
saveToFile("config.conf", data);
更改您的保存到文件((,如下所示
static void saveToFile(std::string& fileName, std::string& text)
{
FILE *file;
file = fopen(fileName.c_str(), "wb");
fwrite(text.c_str(), sizeof(text[0]), text.size(), file);
fclose(file);
}
我尝试了这段代码并且工作正常。
相关文章:
- 使用流处理接收到的数据
- boost::asio UDP 广播客户端仅接收"fast"数据包
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- Java 客户端C++服务器数据发送/接收问题
- CURLOPT_INTERLEAVEFUNCTION回调函数始终接收 nullptr 作为用户数据指针
- 如何接收猫鼬发送的数据?
- 在 MySQL 中运行 HTTP 服务器以从客户端接收数据
- 当接收到不明确的规范时,表示图的邻接列表的数据结构
- 如何使用gRPC在客户端和服务器之间双向发送和接收流元数据
- 这个x86汇编代码是如何接收和存储子程序返回的数据的
- 无法通过套接字发送或接收数据
- 连接UDP套接字,但仍然接收来自其他源的数据报
- 串行读取()不会在没有数据接收的情况下返回值
- 我想使用 I2C 接收从 arduino 到 raspberry pi 的多个数据
- 提升 Asio SSL 无法第二次接收数据(第一次确定)
- boost::asio::async_read 无限循环,接收数据为零字节
- 使用信号处理程序处理从 FIFO 接收的数据
- 以 ns3 为单位计算数据包接收时间
- 用于UDP数据包接收的Qt GUI
- SFML TCP 数据包接收