Thread: passing a struct to a named pipe

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    3

    passing a struct to a named pipe

    First time posting on here...
    I have been struggling with passing data through a pipe and ensuring that it is valid on the other side. I am using braces as delimiters to check the output and this works great for strings...however the struct i have created is using these delimiters and i want to check on the other side whether or not it is valid data. However, when i check for the null character '\0' it always is the next character on the struct that has been passed. In other words i would expect that after i get the '{' delimeter the next character would be whatever is in my struct.

    Code:
                       ptr = msg_buff;
                        offset = 0;
                        while((*ptr != '\0') && (*ptr != '{')){
                           *(bslptr)++;
                            offset++;
                        }
                        if(*ptr == '{'){                     
                            msg_buff=ptr;
                            bytesleft = (MSG_LEN - offset);
                            totalbytes = MSG_LEN - bytesleft;
                        }
                        else if (*bslptr == '\0') {                       
                            offset = 0;               
                            totalbytes = 0;
                            bytesleft = MSG_LEN;
                        }
    And this is what the structure looks like:
    Code:
    typedef struct{
        unsigned char   head;
        unsigned int      code;
        unsigned short  type;
        unsigned short  set;
        unsigned char   tail;
    }MESSAGE;

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When you are passing binary data (the contents of your struct) you really can't use any markers to indicate the content - because for example the value 123 is a '{', so if one of your numeric values contains 123 in some form, that will be seen as an end-marker. And of course, zero isn't a particularly useful value to check for in a numeric binary pattern - you can easily have zero as part of code for example, in your data structure.

    Also beware of alignment holes (aka padding). For example, you have a char followed by an int. a char is usually a single byte, int usually 4 bytes. The latter is usually aligned to an even 4 byte boundary, so you would have three "hole" (or padding) bytes in the structure. You may want to check the sizeof(MESSAGE) vs. what you think the size should be.

    I would say that the best way to pass binary data is to have a dedicated header that contains a "magic" number that is unlikely to be your real data [but DON'T RELY on this not being present in the data-stream]. The header should also contain a length of the data, and if you want to make sure you got the right data, also a checksum (a simple checksum should be sufficient).

    If you really want to use start and/or end markers, you should probably use an escape pattern, so that if the data contains your marker, the data would have to have an "escape" inserted, and then the next byte modified. This is similar to the use of \ in C strings. If you want to use a backslash, you need to use \\, if you want a newline, \n is the way to indicate that.

    A further alternative is of course that you send your data as strings in some way, instead of using a binary form.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    Thank you very much for the informative answer / suggestions. To give just a big of background on it. I am using a fixed size and write that size as well as read the same size on the other end of the pipe. What I suppose one of my many pitfalls was that i expected the while loop to stop on a '\0' character when i should have just traversed the stored buffer looking for '{' and not carring if i hit null. I was worried i would go beyond my address space and cause a seg fault.

    Also when I set the pointer to the new pointer i wasn't sure how it gets back to the original address...but I suppose I really need to read the faq and search around for how that works.

    I think where I got confused was when i tried to print these characters out using a %c on the binary portion it would blow up (print bizzare characters). If i am debugging should i just cast each character as an int and print it out that way?

    Thanks again.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Binary data don't usually print well with %c. Try %02x for each char instead.

    Of course, the other way to know that you are in sync is just to load the entire data packet into a struct, and checking that your start and end markers are what you expect them to be. If they are not, read single bytes from the pipe until you see the start marker, then read it into your buffer again. [I believe pipes within a single machine is very safe anyways, so ther should only be a potential problem if you have cross-machine pipes].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    Very interesting...reading single bytes was exactly what i was doing before but was so slow. I decided to get "smart" about it and try to read what i thought was left in the length of bytes I expect to see. Thanks for the info.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. passing struct vs. reference
    By reakinator in forum C Programming
    Replies: 4
    Last Post: 06-14-2008, 10:11 PM
  3. passing struct
    By Doxygen in forum C Programming
    Replies: 2
    Last Post: 10-26-2006, 03:05 AM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. passing struct to function help
    By staticalloc in forum C Programming
    Replies: 4
    Last Post: 10-06-2004, 08:30 AM