繁体   English   中英

ZMQ message_t.data() 清除数据

[英]ZMQ message_t.data() clear data

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

我设法创建了一个 helloworld 服务器,它可以很容易地打印发送消息的内容,但是我的问题是我无法找到清除请求变量中缓冲区的方法。

这就是我的意思:

当我尝试发送请求时,例如:

tuna

出于某种原因, request.data()返回的指针出于某种原因用 6 个十六进制字符初始化,并且在memcpy结束后发送的消息是:

tuna�

如果我没有输入任何消息作为消息,消息将变为:

�q���

如果我输入足够的字符,字符可以覆盖它。 此外,这些数据保存在变量中,即使我在循环开始时重新初始化message_t变量。 这意味着,如果我发送了一条Disestablishmentarianism的消息,然后又发送了一条Pro的消息,那么得到的消息就是proestablishmentarianism

这是我的代码供参考:

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

编辑:我设法使用以下方法找到了解决方法: snprintf ((char *) request.data (),input.size()+1,"%s", input.c_str() ); 而不是memcpy但我觉得它仍然不能完全回答为什么内存没有被清除的问题。 也许我遗漏了 C++ 的一些基本知识,但即使经过大量搜索,我仍然找不到它。 :(

你应该使用

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

初始化

暂无
暂无

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

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