Passing a structure of arrays to a socket...

Discussion in 'C++' started by Adam Balgach, Sep 18, 2004.

  1. Adam Balgach

    Adam Balgach Guest

    Greetings everyone, ive got a problem ive been working with for quite
    a while and need some help.

    ive got a structure:

    struct Data {
    char *data1;
    char *data2;
    int val1;
    int val2;
    };

    int main() {

    Data myData;
    myData.data1="this is test1";
    myData.data2="this is test2";
    myData.val1=23;
    myData.val2=78;


    now lets assume i have all the code correctly to open and bind a
    datagram packet (UDP) now how do i send this structures values over
    the socket...

    ive been using this code:

    if (sendto(sock, (char *)&myData, sizeof(myData), 0, (struct sockaddr
    *)
    &GatewayAddr, sizeof(GatewayAddr)) != sizeof(myData))
    DieWithError("sendto() sent a different number of bytes than
    expected");

    assuming sock is an int already opened, and GatewayAddr is a structure
    holding the address of the server.

    why isnt this working?

    ive also tried to manually turn the structure into a char* stream by
    doing:

    char *toSend;
    toSend=(char*)malloc(sizeof(myData)+1);
    memcpy(toSend,&myData,sizeof(myData));
    toSend[sizeof(myData)]='\0';

    printf("Packet: %s\n",toSend); //Displays nothing but 'Packet: '

    if (sendto(sock, toSend, sizeof(myData), 0, (struct sockaddr *)
    &GatewayAddr, sizeof(GatewayAddr)) != sizeof(myData))
    DieWithError("sendto() sent a different number of bytes than
    expected");

    which of course also doesnt work.

    Does anyone know how i can accomplish this seemingly simple task?

    Thanks so much.

    Cheers,
    Adam.
    Adam Balgach, Sep 18, 2004
    #1
    1. Advertising

  2. In the structure "struct Data", both structure member "data1" and "data2"
    actually defined as char pointer, when you trying to send via UDP, you only
    send pointers, not the data which the pointer pointing to.
    the overall size of the structure on 32 bytes, which makes ur DieWithError
    occurred.

    2 ways to solve ur problem :

    1. change ur structure becomes similiar to this :
    struct Data {
    char data1[255];
    char data2[255];
    int val1;
    int val2;
    };

    2. You still can use back the original struct, but b4 you send via UDP, you
    got to construct into a "buffer" b4 sending out.

    cheers.

    "Adam Balgach" <> wrote in message
    news:...
    > Greetings everyone, ive got a problem ive been working with for quite
    > a while and need some help.
    >
    > ive got a structure:
    >
    > struct Data {
    > char *data1;
    > char *data2;
    > int val1;
    > int val2;
    > };
    >
    > int main() {
    >
    > Data myData;
    > myData.data1="this is test1";
    > myData.data2="this is test2";
    > myData.val1=23;
    > myData.val2=78;
    >
    >
    > now lets assume i have all the code correctly to open and bind a
    > datagram packet (UDP) now how do i send this structures values over
    > the socket...
    >
    > ive been using this code:
    >
    > if (sendto(sock, (char *)&myData, sizeof(myData), 0, (struct sockaddr
    > *)
    > &GatewayAddr, sizeof(GatewayAddr)) != sizeof(myData))
    > DieWithError("sendto() sent a different number of bytes than
    > expected");
    >
    > assuming sock is an int already opened, and GatewayAddr is a structure
    > holding the address of the server.
    >
    > why isnt this working?
    >
    > ive also tried to manually turn the structure into a char* stream by
    > doing:
    >
    > char *toSend;
    > toSend=(char*)malloc(sizeof(myData)+1);
    > memcpy(toSend,&myData,sizeof(myData));
    > toSend[sizeof(myData)]='\0';
    >
    > printf("Packet: %s\n",toSend); //Displays nothing but 'Packet: '
    >
    > if (sendto(sock, toSend, sizeof(myData), 0, (struct sockaddr *)
    > &GatewayAddr, sizeof(GatewayAddr)) != sizeof(myData))
    > DieWithError("sendto() sent a different number of bytes than
    > expected");
    >
    > which of course also doesnt work.
    >
    > Does anyone know how i can accomplish this seemingly simple task?
    >
    > Thanks so much.
    >
    > Cheers,
    > Adam.
    Desmond Phoon, Sep 18, 2004
    #2
    1. Advertising

  3. Adam Balgach

    Jack Klein Guest

    On 17 Sep 2004 18:55:21 -0700, (Adam Balgach) wrote
    in comp.lang.c++:

    Note that issues about sockets in general are off-topic here and
    belong in a group that supports your particular compiler/OS
    combination because standard C++ has no built-in support for any sort
    of networking at all.

    But in this case, at least part of your problem is C++ related.

    > Greetings everyone, ive got a problem ive been working with for quite
    > a while and need some help.
    >
    > ive got a structure:
    >
    > struct Data {
    > char *data1;
    > char *data2;
    > int val1;
    > int val2;
    > };
    >
    > int main() {
    >
    > Data myData;
    > myData.data1="this is test1";
    > myData.data2="this is test2";


    At this point, your struct contains the addresses of two string
    literals. Those pointer values are almost certainly meaningless to
    the receiver.

    Just as when storing internal data into an ordinary file, the values
    of pointers, that is the addresses of some data items, it not a useful
    thing to store. You need to send the data that those pointers point
    to, not the contents of the pointers themselves, which are the
    addresses of the characters.

    > myData.val1=23;
    > myData.val2=78;


    [snip off-topic socket stuff]

    > why isnt this working?
    >
    > ive also tried to manually turn the structure into a char* stream by
    > doing:
    >
    > char *toSend;
    > toSend=(char*)malloc(sizeof(myData)+1);
    > memcpy(toSend,&myData,sizeof(myData));
    > toSend[sizeof(myData)]='\0';


    [snip more off-topic socket stuff]

    > which of course also doesnt work.


    Because the characters that you have copied into the allocated array
    still contains the contents of the pointers, that is the addresses of
    the character string literals, not the actual characters.

    > Does anyone know how i can accomplish this seemingly simple task?
    >
    > Thanks so much.
    >
    > Cheers,
    > Adam.


    You need to change your structure definition to contain arrays of
    char, and copy the chars into the arrays, perhaps with std::strcpy.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Sep 18, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,767
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    948
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    531
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    648
    Steve Holden
    Feb 1, 2009
  5. Replies:
    2
    Views:
    256
    Rui Maciel
    Dec 12, 2012
Loading...

Share This Page