2

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. :(

Community
  • 1
  • 1
Liang
  • 383
  • 4
  • 13
  • ZeroMQ sockets have internal "line-code" that helps the **Formal Commmunication Patterns Framework** deliver it's unique set of **Behavioral Model(s)** respective to the selected Library Primitive(s). You might want to make your post compatible with StackOverflow policy to include a **MCVE** = **M**inimum **C**omplete **V**erifiable **Example** piece of code, so as to both repeat an objected trouble and validate any solution proposed to fix it. You code is not complete. There is missing both the ZeroMQ socket model setup and the infrastructure dismantling & termination.**+Enjoy StackOverflow** – user3666197 Jul 26 '15 at 12:01
  • Hey I've added the more complete code, thanks – Liang Jul 26 '15 at 12:08

1 Answers1

0

you should use

message_t(void* data, size_t len)

//std::string tmp = "example";
//message_t(tmp.c_str(), tmp.size());

to init

Z.Yong
  • 1