简体   繁体   English

C ++:通过命名管道发送对象

[英]C++ : Send an object through a named pipe

I try to send an object through a named pipe, between two process, in C++ with ifstream and ofstream. 我尝试在C ++中使用ifstream和ofstream在两个进程之间通过命名管道发送对象。 I have read and try so many things but my research has led to nothing. 我已经阅读并尝试了很多东西,但是我的研究没有结果。

I block during the serialization of my object. 我在对象序列化期间阻塞。 When I try to cast and send to my named pipe, I can not recover my object to his normale state. 当我尝试投射并发送到命名管道时,无法将对象恢复到他的正常状态。

I try something with this code but the object is not full after its passage in the named pipe : 我尝试使用此代码进行操作,但是对象在命名管道中通过后未满:

#include <string.h>
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>

class Obj {
public:
    std::string text1;
    std::string text2;
};

int main() {

    mkfifo("fifo", 0666);

    if (fork() == 0) //Receiving Side
    {

        std::ifstream fifo("fifo", std::ofstream::binary);

        //Re-make the object
        Obj *tmp = new Obj();
        char *b = new char[sizeof(*tmp)];

        //Receive from named pipe
        fifo >> b;

        //Recover the object
        memcpy(&*tmp, b, sizeof(*tmp));

        //Display object content
        std::cout << tmp->text1 << std::endl << tmp->text2 << std::endl;

        //!\ Output = "Some \n" /!\\

        fifo.close();
        delete tmp;
        delete[] b;
    }
    else //Sending Side
    {
        std::ofstream fifo("fifo", std::ofstream::binary);

        //Create the object
        Obj *struct_data = new Obj();
        struct_data->text1 = "Some text";
        struct_data->text2 = "Some text";

        char *b = new char[sizeof(*struct_data)];

        //Serialize the object
        memcpy((void *)b, &*struct_data, sizeof(*struct_data));

        //Send to named pipe
        fifo << b;

        fifo.close();
        wait(NULL);
        delete[] b;
    }

    //delete struct_data;
    return (0);
}

Does someone could give me a tip or en example ? 有人可以给我小费或举个例子吗?

Thanks ! 谢谢 ! :) :)

You will need to serialize your object properly. 您将需要正确序列化您的对象。 Something like this (just did one member, the rest will be left as an excercise to the reader): 这样的事情(只有一名成员,其余的留给读者练习):

#include <iostream>
#include <fstream>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>

class Obj
{
  public:
    std::string text1;
    std::string text2;

  friend std::ostream& operator<<(std::ostream &os, const Obj& o);
  friend std::istream& operator>>(std::istream &os, Obj& o);
};

std::ostream& operator<<(std::ostream &os, const Obj& o)
{
  os << o.text1.length();
  os << o.text1;
  return os;
}

std::istream& operator>>(std::istream &is, Obj& o)
{
  size_t length;
  is >> length;
  char* tmp = new char[length];
  is.get(tmp, length+1); 
  o.text1 = tmp;
  delete[] tmp;
  return is;
}

static const char* myfifo = "myfifo";

int main(int argc, char *argv[])
{
  mkfifo(myfifo, 0666);

  if (argc > 1)
  {       
    std::ifstream infifo(myfifo, std::ifstream::binary);

    Obj *tmp = new Obj();

    infifo >> *tmp; 
    infifo.close();

    std::cout << "Done reading : [" << tmp->text1 << "]" << std::endl;
  }
  else
  {
    std::ofstream outfifo(myfifo, std::ofstream::binary);

    Obj *struct_data = new Obj();
    struct_data->text1 = "Some text1";
    struct_data->text2 = "Some text2";

    outfifo << *struct_data;
  }
  return 0;
}

For more on serialization see https://stackoverflow.com/a/26337239/2742863 有关序列化的更多信息,请参见https://stackoverflow.com/a/26337239/2742863

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

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