C++;:通过命名管道发送对象 我尝试通过一个命名管道,在两个进程之间,在C++中与IFStand和OFFROW发送一个对象。我读过很多书,尝试过很多东西,但我的研究却一无所获

C++;:通过命名管道发送对象 我尝试通过一个命名管道,在两个进程之间,在C++中与IFStand和OFFROW发送一个对象。我读过很多书,尝试过很多东西,但我的研究却一无所获,c++,serialization,casting,named-pipes,fifo,C++,Serialization,Casting,Named Pipes,Fifo,我在对象序列化期间阻塞。 当我尝试强制转换并发送到命名管道时,我无法将对象恢复到其正常状态 我尝试使用此代码执行某些操作,但对象在命名管道中通过后未满: #include <string.h> #include <iostream> #include <unistd.h> #include <fstream> #include <sys/types.h> #include <sys/stat.h> #include <

我在对象序列化期间阻塞。 当我尝试强制转换并发送到命名管道时,我无法将对象恢复到其正常状态

我尝试使用此代码执行某些操作,但对象在命名管道中通过后未满:

#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);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
Obj类{
公众:
std::stringtext1;
std::stringtext2;
};
int main(){
mkfifo(“fifo”,0666);
if(fork()==0)//接收端
{
std::ifstream fifo(“fifo”,std::ofstream::binary);
//重新制作对象
Obj*tmp=新Obj();
char*b=新字符[sizeof(*tmp)];
//从命名管道接收
先进先出>>b;
//恢复对象
memcpy(&*tmp,b,sizeof(*tmp));
//显示对象内容
std::cout text1 text2=“一些文本”;
char*b=新字符[sizeof(*struct_data)];
//序列化对象
memcpy((void*)b和*struct_data,sizeof(*struct_data));
//发送到指定管道

fifo您需要正确序列化您的对象。类似这样的事情(只做了一个成员,其余的将留给读者作为练习):

#包括
#包括
#包括
#包括
#包括
Obj类
{
公众:
std::stringtext1;
std::stringtext2;
friend std::ostream&operator(std::istream&os,Obj&o);
};
标准::ostream和运算符长度;
char*tmp=新字符[长度];
is.get(tmp,长度+1);
o、 text1=tmp;
删除[]tmp;
回报是;
}
静态常量char*myfifo=“myfifo”;
int main(int argc,char*argv[])
{
mkfifo(myfifo,0666);
如果(argc>1)
{       
std::ifstream infifo(myfifo,std::ifstream::binary);
Obj*tmp=新Obj();
infifo>>*tmp;
infifo.close();

std::无法向我们显示序列化代码。字节格式是否有文档记录?如果有,请向我们显示文档。如果没有,请记录它。相信我,记录任何协议都是值得的——最好是在编码之前。当你说“cast and send”时…您实际上是在序列化还是只是试图发送原始字节?对象是什么样子的?您的问题可能与管道无关(命名与否)。当读者从stdin接收对象并将它们与
|
连接时,您可以尝试让发送方写入stdout。这样可以节省大量的设置管道和其他工作。@Bross没有代码来序列化对象!一些小问题:(1)为什么
操作符>
的友元声明带有
常量为什么不在
operator>
中返回
os
?(3)为什么在
operator>
中命名输入流
os
(它是输出流的缩写,IIUC)?另外,它不是
os.get(tmp,length+1)
off-by-1错误?@OrenMilman,你说得对,好的命名对可读性很重要。那里有一些草率的代码,我想我现在已经修复了。我不认为存在“off-by-1”错误。根据文档,(),读取“直到(n-1)个字符被提取出来”。
#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;
}