Click to See Complete Forum and Search --> : Problem sending a C++ class via UDP (wrong size?) in C++ .NET


MetalGod
June 13th, 2004, 01:59 AM
Hey there,

I'm trying to send an instance of a class via a UDP packet.

I'm using this method--

udpclient.Send( (char*)&message_class, sizeof(MessageClass));

This creates an error, because when I cast my class to the char*, it ends up only being about 4 bytes long, which is an incorrect size. When I do a sizeof(MessageClass), it correctly reports it as 128bytes. I want to send the entire 128bytes of the class.

I know that casting a class to a char* is an unsafe way of serializing an object, but unfortunately the listener program was written by someone else and deserialzes the class with this method.

I'm porting the sender program from UNIX style C that used winsock.h, I'm trying to used the Managed C++ .NET libraries.

Thanks!

Dave

kuphryn
June 13th, 2004, 11:45 AM
UDP is a connection-less or message-based protocol. I recommend TCP for raw data.

As for the sending problem, it could be that you need to keep sending until all bytes have been sent.

Kuphryn

MetalGod
June 13th, 2004, 12:37 PM
I believe I must use UDP because the listener program (not written by me) is using UDP.

How do I keep sending until all the bytes have been sent?

kuphryn
June 13th, 2004, 01:20 PM
Allocate temporary memory and store the entire class structure there. Traverse until you've sent all of the structure.

Kuphryn