简体   繁体   English

ZMQ message_t.data() 清除数据

[英]ZMQ message_t.data() clear data

In reference to this question: Zeromq: How to access tcp message in c++参考这个问题: Zeromq: How to access tcp message in c++

I managed to create a helloworld server that can print the contents of the sent messages pretty easily, however my issue is that I can't figure out a way to clear the buffer in the request variable.我设法创建了一个 helloworld 服务器,它可以很容易地打印发送消息的内容,但是我的问题是我无法找到清除请求变量中缓冲区的方法。

Here's what I mean:这就是我的意思:

When I try and send a request such as:当我尝试发送请求时,例如:

tuna

For some reason the pointer returned by request.data() is initialised with 6 hexadecimal characters for some reason, and the message sent, after a memcpy ends up being:出于某种原因, request.data()返回的指针出于某种原因用 6 个十六进制字符初始化,并且在memcpy结束后发送的消息是:

tuna�

If I enter nothing as the message, the message becomes:如果我没有输入任何消息作为消息,消息将变为:

�q���

If I enter enough characters, the characters can overwrite it.如果我输入足够的字符,字符可以覆盖它。 Additionally this data is kept in the variable, even if I re-initialise the message_t variable at the start of the loop.此外,这些数据保存在变量中,即使我在循环开始时重新初始化message_t变量。 This means that if I send a message of Disestablishmentarianism , and subsequently send another message of Pro , the resultant message is proestablishmentarianism .这意味着,如果我发送了一条Disestablishmentarianism的消息,然后又发送了一条Pro的消息,那么得到的消息就是proestablishmentarianism

Here's my code for reference:这是我的代码供参考:

    //sender loop
        using namespace std;
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_REQ);

int main(){
    socket.connect("tcp://localhost:28641"); //28641 is the set to be standardised for this kind of communication
    string input = "";
    cout << "this is the client for communicating with Thalamus.\nTo start, input the name of the module to be used:";

    while(true){
        std::cout << "Type a config command:" << std::endl;
        getline(cin, input);
        zmq::message_t request(input.size()+1);
        //(char *)request.data() = ' ';
        std::string req = std::string(static_cast<char*>(request.data()), request.size());
        std::cout << req << std::endl;
        //std::cout << input;
        memcpy ((void *) request.data (), input.c_str(), input.size());
        socket.send (request);
        printf("%s\n",(char*)request.data());

        //  Get the reply.
        zmq::message_t reply;
        socket.recv (&reply);
        std::cout << "response:" << std::endl; 
        printf("%s\n",(char *)reply.data());
        //printf("%s", "moo");


    }


    return 0;
}


    //receiver loop
void Config_Interface(){
    //Config_Interaction uses ipc protocols as an interface to controlling and configuring the execution of the program.
    /*
        requirements:
            start stream
            stop stream
            pause/resume
    */
    zmq::context_t config_context(1);
    zmq::socket_t config_socket(config_context, ZMQ_REP);
    config_socket.bind ("tcp://*:28641");


    while(true){
        zmq::message_t request;

        config_socket.recv(&request);
        std::cout << "config update msg received:" << std::endl;
        std::string req = std::string(static_cast<char*>(request.data()), request.size());
        std::cout << req << std::endl;
        //Config_Parse(request.data()); //Function that will parse the command and update the config variables.
        std::cout << "--updated config--";

        zmq::message_t reply(7);
        memcpy ((void *) reply.data (), "got it", 6);
        config_socket.send (reply);
    }
}

Edit: I've managed to find a work around by using: snprintf ((char *) request.data (),input.size()+1,"%s", input.c_str() );编辑:我设法使用以下方法找到了解决方法: snprintf ((char *) request.data (),input.size()+1,"%s", input.c_str() ); instead of memcpy but I feel as though it still doesn't quite answer the question of why the memory isn't cleared.而不是memcpy但我觉得它仍然不能完全回答为什么内存没有被清除的问题。 Maybe I'm missing something fundamental to C++, but even after a ton of searching I still can't find it.也许我遗漏了 C++ 的一些基本知识,但即使经过大量搜索,我仍然找不到它。 :( :(

you should use你应该使用

 message_t(void* data, size_t len) //std::string tmp = "example"; //message_t(tmp.c_str(), tmp.size());

to init初始化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM