-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Adam Balgach wrote:
> lets say i define some struct:
>
> struct myStruct {
> int a;
> int b;
> char* msg1;
> char* msg2;
> }
>
> now in the main body of code, i populate this
>
> myStruct testS;
> testS.a=2
> testS.b=4
> testS.msg1="test1"
> testS.msg2="test2"
>
>
> now i want to send this entire structure over a UDP socket and recieve
> it on another computer (same internetal network, so int size is not a
> factor)
>
> i am familiar with opening and closing a UDP sockets and sending
> simple plain text messages stored in a char* array over them, but dont
> ahve a clue where to begin with structures. Ive looked on the
> internet, and nothing is really helpful.
>
> I tried to convert the entire structure to a char array using
> somehting like
> (char *)&testS
>
> but clearly that didnt work.
Well, it should have worked, for some value of 'work'.
The send should work properly, given the cast, and the (presumed)
sizeof(testS) as the length, and your receiving program should have
received the contents of the structure in a UDP datagram.
However, some of the contents of that datagram are going to be useless
to the receiver, because you aren't sending what you think you are
sending. Take another look at your structure definition, and pay
particular attention to the msg1 and msg2 variables.
You'll notice that these two variables are pointers. Your sending
program populates these two variables with addresses local to the
sending program, then sends the structure to the receiving program. The
problem is that the receiving program doesn't have any data at the
addresses pointed to by the two variables, and thus you don't get the
proper data transferred.
> any ideas on what to do?
change your structure so that you transfer char arrays, rather than
pointers to char.
struct myStruct {
int a;
int b;
char msg1[64];
char msg2[64];
};
and populate the arrays before you send
{
myStruct testS;
testS.a=2;
testS.b=4;
strcpy(testS.msg1,"test1");
strcpy(testS.msg2,"test2");
/* etc */
}
now, when you send, you send data, not pointers. The receiving program
will receive data, and be able to process it.
> Thanks,
>
> Cheers,
> Adam.
- --
Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
iD8DBQFBSzzXagVFX4UWr64RAhVoAKCCiJaduniOY//BfJtAwMyKdE851ACfa+Y3
ZUlQc09QI+tqoGAVC+G8UKA=
=0N8K
-----END PGP SIGNATURE-----
|