如何接收猫鼬发送的数据?

时间:2019-12-02 17:07:29

标签: c++ c 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,

万一有人知道如何解决此问题,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为您的问题是由于将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);
}

我尝试了此代码,并且工作正常。