2016-04-15 82 views
0

我尝试通过命名管道,在两个进程之间,使用ifstream和ofstream在C++中发送对象。我已经阅读并尝试了很多东西,但是我的研究一无所获。C++:通过命名管道发送对象

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

我尝试一些与此代码,但它的命名管道通过后的对象是未满:

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

是否有人可以给我一个提示或EN例子吗?

谢谢! :)

+1

我们展示的序列化代码。是否记录了字节格式?如果是这样,请告诉我们文档。如果没有,请记录下来。相信我,这是值得努力的文件任何协议 - 理想之前,你编写它。 –

+0

当你说“铸造和发送”......你是实际序列化,还是只是试图发送原始字节?你的物体是什么样的? – Useless

+0

您的问题可能与管道无关(指定或不指定)。您可以尝试让发送者写入标准输出,而阅读器从标准输入接收对象并使用'|'连接它们。节省了很多努力来设置管道,而不是。 –

回答

0

您将需要正确序列化您的对象。像这样的东西(只是做了一个成员,其余的将被保留为锻炼; Tibial读者):

#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, const 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 &os, Obj& o) 
{ 
    size_t length; 
    os >> length; 
    char* tmp = new char[length]; 
    os.get(tmp, length+1); 
    o.text1 = tmp; 
    delete[] tmp; 
} 

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 text"; 
    struct_data->text2 = "Some text"; 

    outfifo << *struct_data; 
    } 
    return 0; 
} 

更多关于系列化看到https://stackoverflow.com/a/26337239/2742863