pucek7
pucek7

Reputation: 31

How send a structure through fifo

HI!

I am trying to pass whole structure from one program and read it in another using fifo. I'm using read and write functions.

I had a problem with putting my structure into this functions. When I did it and tried to send and receive I get an error (core dump) or I recived some trash. I dont know exactly, where my problem take place (in receiver or sender). How can I send/receive my structure, or what i have wrong in my code.

Here is my code...

Receiver

struct data
{
    char* message;
    int size;
    vector <times> prog;
};

int if_fifo(char* name)
{
    struct stat info;
    int score = stat(name,&info);
    if(S_ISFIFO(info.st_mode))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int fifo_in(char* name)
{
    data msg;
    int pip;
    pip = open(name, O_RDONLY | O_NONBLOCK);
    while(1)
    {
        int hr = read(pip,&msg,sizeof(msg));
        if(hr != 0)
        {
            cout << "Message:   " << msg.message << endl;
        }
    }
    cout << "O.K." << endl;
    return 0;
}

int main(int argc, char** argv) {
int c, status_in, status_out;
char* input;
char* output;
float del;

if(argc < 5)
{
    cout << "Za malo podanych parametrow" << endl;
    return 1;
}
else
{
    while ((c = getopt(argc, argv, "iod:")) != -1)
    {
        switch (c)
        {
        case 'i':
            input = argv[2];
            status_in = if_fifo(input);
            break;
        case 'o':
            output = argv[3];
            status_out = if_fifo(output);
            break;
        case 'd':
            del = atof(argv[4]);
            break;
        case '?':
            printf("UKNOWN");
        }
    }
}

if(status_in == 1)
{
    return fifo_in(input);
}
else
{
    cout << "It isnt fifo!!" << endl;
}

return 0;
}

And sender:

struct data
{
    char* message;
    int size;
    vector <times> prog;
}msg;

int if_fifo(char* name)
{
    struct stat info;
    int score = stat(name,&info);
    if(S_ISFIFO(info.st_mode))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int fifo_out(char* name)
{
    msg.message = "To jest to!!";
    msg.size = sizeof(msg.message);
    int pip;
    pip = open(name, O_WRONLY);
    if(  pip == -1 )
    {
        perror("Error: open( ): ");
        return 1;
    }
    write(pip,&msg,sizeof(msg));
    return 0;
}

int main(int argc, char** argv) {
    int c, status_out;
    char* output;

    if(argc < 3)
    {
        cout << "Za malo podanych parametrow" << endl;
        return 1;
    }
    else
    {
        while ((c = getopt(argc, argv, "o:")) != -1)
        {
            switch (c)
            {
                case 'o':
                    output = argv[2];
                    status_out = if_fifo(output);
                    break;
                case '?':
                    printf("UKNOWN");
            }
        }
    }

    if(status_out == 1)
    {
        return fifo_out(output);
    }   
    return 0;
}

Upvotes: 0

Views: 1430

Answers (1)

pm100
pm100

Reputation: 50190

you cannot just send memory structures from one program to another. You have to do whats called 'serialization' ie convert the struct into a byte stream that represents the structure. There are many, many serialization techniques: ASN1/ Ber, XML, JSON, Google protocol buffs, roll your own.

Just so you know why this is. The field message in your struct is actually a pointer, when you send this pointer to another program it points to the same address but in the receiver prgram not the sender. That address likely doesnt exist and certainly does not contain the string you had in the sender program.

Upvotes: 3

Related Questions