Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Can I send a C struct to a socket?

  1. #1
    Join Date
    Mar 2006
    Location
    Lisbon, Portugal
    Beans
    1,909

    Can I send a C struct to a socket? [SOLVED]

    I'm trying to send a struct to a DGRAM socket. Here's what i'm doing:

    client (sends struct)

    Code:
    n = sendto(socket_fd,(const char*)&mypdu,sizeof(mypdu),0,
        		(struct sockaddr *)&server_addr,sizeof(server_addr));
    server

    Code:
    n=recvfrom(socket_fd,buffer,sizeof(mypdu),0,
    				(struct sockaddr *)&from,&fromlen);
    but in the client, i initialized one of the fields with 100, and in the server end, that field has the value 3 =\

    Can I send the whole struct without sending it field by field?
    Last edited by raul_; November 16th, 2007 at 02:20 AM.
    My blog | Arch User | Linux user #439142 | Ubuntu user #10974
    "God is real unless you declare it as integer"

  2. #2

    Re: Can I send a C struct to a socket?

    You will have to serialize the thing to send, and the receiver will need to reassemble the thing, very painfull.

  3. #3
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: Can I send a C struct to a socket?

    You shouldn't send a whole struct at a time, in fact you shouldn't even send integers (int) "as-is".


    The problem with structs is that depending on the compiler, on the OS, on the architecture, ... there are different padding conventions that apply.


    As an example:

    Code:
    struct Foo
    {
      char foo;
      int bar;
    };
    sizeof(struct Foo) is not guaranteed to (and in real world will almost never) be equal to sizeof(char)+sizeof(int) due to those padding issues.


    Moreover, depending on the CPU architecture there are problems of endianness too (most or least significant byte first?).


    The rule of thumb being:
    - send structs field by field
    - if you have to transfer integers, use hton / ntoh functions (aka. Host To Network / Network To Host) which correctly convert byte order.

    Or, use plain text protocols.
    Not even tinfoil can save us now...

  4. #4
    Join Date
    Mar 2006
    Location
    Lisbon, Portugal
    Beans
    1,909

    Re: Can I send a C struct to a socket?

    Thanks.

    Very painful indeed
    My blog | Arch User | Linux user #439142 | Ubuntu user #10974
    "God is real unless you declare it as integer"

  5. #5
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Can I send a C struct to a socket?

    I would memcopy the struct into a buffer and send the buffer
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  6. #6
    Join Date
    Feb 2007
    Beans
    236

    Re: Can I send a C struct to a socket?

    Listen to aks44.

    I'll just add that, even if you use datatypes that you're sure all of your networked devices regard as the same bitsize (for example, I can't think of a current architecture that doesn't consider "char" to be 8 bits), same byte order (again, "char" and "unsigned char" relieve you of that worry), or padding, you absolutely, absolutely cannot send any "pointer" to another machine. For example, assume you have the following two struct definitions:

    Code:
    struct Something {
       char buf[8];
    }
    
    struct SomethingPtr {
      struct Something *something;
    }
    You can't do something like sending these two structs:

    Code:
    mySomething = {"text"};
    MySomethingPtr = {&mySomething};
    The mySomething address on one machine is going to be completely different on the other machine. You have to do something people often call "flattening the struct". You replace that pointer field with something that indicates to the other machine that it needs to reinsert a pointer to the mySomething struct you previously sent over.

    You may want to take a look at how Microsoft's COM "marshalls" data between processes. There are examples of flattening structs there.
    Last edited by j_g; November 16th, 2007 at 12:33 AM.

  7. #7
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,267
    Distro
    Kubuntu

    Re: Can I send a C struct to a socket?

    Quote Originally Posted by kknd View Post
    You will have to serialize the thing to send, and the receiver will need to reassemble the thing, very painfull.
    This is the correct way to do things. although I would not describe it as "painful". It's just another opportunity to code more.

    Aks44 was also correct in that endianess (sp?) is a factor. If you are transferring data from an Intel architecture to a PPC (power PC) architecture, then you will need to accommodate that the bytes in the words are swapped. M$'s .NET and CORBA handle this automagically. You can also do it yourself with minor effort.

    Back to the serialize/unserialize procedure... you will need to perform a deep-copy of your structure into a buffer, and if applicable, indicate the size of the buffer and sizes of any variable-length fields within the same buffer.

    Once this buffer is sent across the wire, the recipient should be able to parse through the received buffer to reconstruct the original data structure.

  8. #8
    Join Date
    Mar 2006
    Location
    Lisbon, Portugal
    Beans
    1,909

    Re: Can I send a C struct to a socket?

    this code is to work between Linux ix86 systems, so I don't think I have that sort of problems. I'm glad to say that it was fairly painless. My structure has mostly char* fields, one int (a cast to (const char*) did the trick), and I created a function to convert a list to a string, so I can send it in a buffer.

    It's nice to have so many good responses in a relative short amount of time, in a subject that has almost anything, if anything at all, to do with Ubuntu

    Thanks
    My blog | Arch User | Linux user #439142 | Ubuntu user #10974
    "God is real unless you declare it as integer"

  9. #9
    Join Date
    Mar 2006
    Location
    Lisbon, Portugal
    Beans
    1,909

    Re: Can I send a C struct to a socket? [SOLVED]

    marked it as solved
    My blog | Arch User | Linux user #439142 | Ubuntu user #10974
    "God is real unless you declare it as integer"

  10. #10
    Join Date
    Jun 2010
    Beans
    1

    Re: Can I send a C struct to a socket?

    Hello,

    Can I have the source code please ? I am also having the same problem. I am not familiar with serialization .

    My structure is something like this :

    struct SecurityEventNotice
    {
    int id;
    int timestamp;
    char *description;
    char *source;
    }

    I need to pass this entire structure through a socket and also the server should be able to unmarshall it.

    Can anyone help ??

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •