Passing a structure of arrays to a socket...

A

Adam Balgach

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.
 
D

Desmond Phoon

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.
 
J

Jack Klein

On 17 Sep 2004 18:55:21 -0700, (e-mail address removed) (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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top