Sending Native Struct via TCP
-
I need to communicate with a non-windows platform via TCP. The message will be a unmanaged data structure. How can I encode this into a managed type that the Socket.Send command can handle? The message must be decoded on the other platform, which won't have the .NET framework.
-
I need to communicate with a non-windows platform via TCP. The message will be a unmanaged data structure. How can I encode this into a managed type that the Socket.Send command can handle? The message must be decoded on the other platform, which won't have the .NET framework.
You'd need to create an array of Bytes (for the Socket::Send()) and "serialize" a managed type into a stream of bytes (representing the structure) in the array. A couple things to keep in mind: Structure packing can be different on different languages/platforms. This means you have to ensure each of the structure's members is at the appropriate offset in the Byte array. The BitConverter class can help for converting managed base types to a stream of byes, but you need to deal with byte order (endian-ness) of multi-byte types if the other platform is different. If this is an issue, multi-byte integer types sould be sent using network byte order. IPAddress::HostToNetworkOrder() and IPAddress::NetworkToHostOrder() can do the conversion but the other end needs to know to convert the sent network-order bytes back to host-order. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: