2016-04-15 65 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