Hi,
I am just getting back in to C++ after 10 years not doing any, contributing to an open source project. I'm adding in some functionality and am hitting a road block.
I need to send a multicast packet out on the network that is structured in a certain way. I have the definition, and know what data is going in each byte. I can successfully send a message using multicast, I now just need to send the right message.
I have used a char array to hold the message, as each char represents 1 byte, and I can transmit the array.
I am having trouble putting all of the data in the right place though. If my source data is a string, then I seem to be able to convert it, but if it is a short or int, then I keep getting errors when compiling. Similarly, two of the lines, (version and type) i initially tried using char arrays with a length of one.
What am I doing wrong? Should I be using memcpy or a different function, or even be doing this in a totally different way altogether?
This is the code that I am using, along with the packet structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
//Construct a Zone Query packet
// 4 bytes - Signature "Ohz " = 0x6f, 0x68, 0x7a, 0x20
// 1 bytes - Version = 1
// 1 bytes - Type (0 = Zone Query, 1 = Zone Uri)
// 2 bytes - Entire message length = 12 + zone length
// 4 bytes - Length in bytes of the zone ID
// n bytes - Zone ID to query
//This value will be passed in to the function
string zoneID = "714f9b83b2beb170b19db6a281d2f9512";
//Calculate all of the values that we need
short packetLength = 4 + 1 + 1 + 2 + 4 + strlen(zoneID.c_str());
int zoneIDLength = strlen(zoneID.c_str());
char buffer[packetLength];
string headerText = "Ohz ";
string version = "1";
string messageType = "0";
//Build the packet
memcpy(buffer + 0, headerText.c_str(), 4);
memcpy(buffer + 4, version.c_str(), 1);
memcpy(buffer + 5, messageType.c_str(), 1);
memcpy(buffer + 6, packetLength, sizeof(packetLength));
memcpy(buffer + 8, zoneIDLength, sizeof(zoneIDLength));
memcpy(buffer + 12, zoneID.c_str(), zoneIDLength);
LOGDEB("Buffer: " << buffer << " : Ended" << endl);
|
The errors that I get are:
error: invalid conversion from ‘short int’ to ‘const void*’ [-fpermissive]
memcpy(buffer + 6, packetLength, sizeof(packetLength));
^
error: initializing argument 2 of ‘void* memcpy(void*, const void*, size_t)’ [-fpermissive]
__NTH (memcpy (void *__restrict __dest, const void *__restrict __src,
^
error: invalid conversion from ‘int’ to ‘const void*’ [-fpermissive]
memcpy(buffer + 8, zoneIDLength, sizeof(zoneIDLength));
^
error: initializing argument 2 of ‘void* memcpy(void*, const void*, size_t)’ [-fpermissive]
__NTH (memcpy (void *__restrict __dest, const void *__restrict __src,
Any guidance will be much appreciated!