Networking Forums

Networking Forums > Computer Networking > Linux Networking > Sending a struct over a UDP socket...

Reply
Thread Tools Display Modes

Sending a struct over a UDP socket...

 
 
Adam Balgach
Guest
Posts: n/a

 
      09-17-2004, 07:22 PM
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.

any ideas on what to do?
Thanks,

Cheers,
Adam.
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a

 
      09-17-2004, 07:36 PM
-----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-----
 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sending socket descriptor to another pid Beagle Linux Networking 2 07-09-2008 09:56 PM
How can I send a struct containing a char * with a socket? Patrick Lam Linux Networking 5 04-03-2006 08:54 PM
Sending datagram in raw socket Vicky Linux Networking 0 10-25-2004 10:25 AM
Sending HTML page with a socket Farid Benzakour Linux Networking 1 12-11-2003 04:07 PM
Sending and reading big data by socket cyril Linux Networking 1 08-28-2003 03:58 PM



1 2 3 4 5 6 7 8 9 10 11