Sending a struct over TCP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack

    #1

    Sending a struct over TCP

    Hi all,
    I have a problem. I am writing a client server app in C. In my client
    program, I have a simlple struct similiar to:

    struct mySt{
    int id;
    char val;
    struct mySt *next;

    };

    I want to be able to fill out the variables in this struct and then
    send it to the server.

    I do this using:
    struct mySt pStruct;
    pStruct.id = 1;
    pStruct.val = 'a';
    send ( sd, &pStruct, sizeof(struct pStruct), 0);

    on the server though, even though I do receive this info. I have no
    idea how to parse. I recieve a data in a variable of the same type
    (struct mySt), but I don't know how to read it / parse it. Is it read
    into char or can I do something like:
    struct mySt **receivedPacke t;
    int id = receivedPacket[0];

    Thanks

  • Ian Collins

    #2
    Re: Sending a struct over TCP

    Jack wrote:
    Hi all,
    I have a problem. I am writing a client server app in C. In my client
    program, I have a simlple struct similiar to:
    >
    Please don't multi-post on Usenet, cross-post if you must, but don't
    multi-post. You also asked this on comp.unix.progr ammer, which is
    probably where it should be answered.

    --
    Ian Collins.

    Comment

    • Tor Rustad

      #3
      Re: Sending a struct over TCP

      Jack wrote:
      Hi all,
      I have a problem. I am writing a client server app in C. In my client
      program, I have a simlple struct similiar to:
      >
      struct mySt{
      int id;
      char val;
      struct mySt *next;
      >
      };
      Sending raw struct's over the wire is a really bad idea, even between
      homogeneous systems.

      Depending on the compiler used, different padding may be used.

      So don't do that... for more advanced protocols ASN.1 is typically used,
      but for simpler applications, you can just define endian order of
      binary data, and pass each struct member along the wire.

      --
      Tor

      Comment

      Working...