简体   繁体   中英

ZMQ message_t.data() clear data

In reference to this question: 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.

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:

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. This means that if I send a message of Disestablishmentarianism , and subsequently send another message of Pro , the resultant message is 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() ); instead of memcpy but I feel as though it still doesn't quite answer the question of why the memory isn't cleared. Maybe I'm missing something fundamental to C++, but even after a ton of searching I still can't find it. :(

you should use

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

to init

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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