首页 > 解决方案 > 如何在猫鼬中接收发送的数据?

问题描述

我正在使用 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,

如果有人知道如何解决这个问题,我将非常感谢任何信息。

标签: c++cmongoose

解决方案


我认为您的问题是由于将 char* 复制到 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);

如下更改您的 SaveToFile()

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);
}

我尝试了这段代码并且工作正常。


推荐阅读