emileej Posted August 12, 2004 Report Posted August 12, 2004 My struct: typedef struct SEncryptionRequestPacket{ Uint8 type; //0x00 Uint8 subtype; //0x01 Uint32 key; Uint16 version; //0x0001 }TEncryptionRequestPacket;And its use:void CSSConnection::SendEncryptionRequest(){ Log("SendEncryptionRequest"); ClientPackets::TEncryptionRequestPacket packet; packet.type=0x00; packet.subtype=0x01; packet.key=-(rand() % 0x7FFFFFFF); packet.version=0x0001; //<debug> char buff[11]; Log(((string("Sending ")+itoa(sizeof(ClientPackets::TEncryptionRequestPacket),buff,10))+" bytes").c_str()); //</debug> socket->Send((const char*)&packet,sizeof(ClientPackets::TEncryptionRequestPacket)); }My problem: For some reason sizeof returns 12 rather than the 8 I would expect... Now why is that?
50% Packetloss Posted August 12, 2004 Report Posted August 12, 2004 http://msdn.microsoft.com/library/default....predir_pack.asphttp://www.absoft.com/Products/Compilers/C...ef/rnpgpack.htm
emileej Posted August 13, 2004 Author Report Posted August 13, 2004 Thanks I am experiencing some trouble though. As I understand it, what I want is:#pargma pack(nopack)But that is apparently not supported in gcc. I get the following warning: warning: unknown action 'nopack' for '#pragma pack' - ignored I can reduce the size to 10 by using#pargma pack(1)but that is still not the size I would expect. I could, however, just ignore the extra bytes when sending my packets, but how should I the go about filling a struct with data from an incomming packet? What should I do about the extra bytes in the struct?
50% Packetloss Posted August 13, 2004 Report Posted August 13, 2004 Your processor has a 32 bit word (4bytes). So no matter if you specify a 1 byte variable, that variable is still going to be placed in a memory space that is 4 bytes long but the variable will be treated as if the memory space was only 1 byte large. The reason for even having 1 btye variables is for backwards compatability, old processors were 8 and 16 bits, programs built on those processors should still work on 32 and 64 bit chips. Sorry i couldnt help you with the packets stuff, i dont know enough about how winsock works
emileej Posted August 14, 2004 Author Report Posted August 14, 2004 Oh so thats what pack is all about. Now it actualy makes sense My only worry is if I will be able to fill in my values like this:char *cdata=GetExcitingDataFromTheServer(); TEncryptionRequestPacket pdata; memcpy(&pdata,cdata,sizeof(TEncryptionRequestPacket));
Smong Posted August 16, 2004 Report Posted August 16, 2004 You can do that so long as you do #pragma pack(1) before you define TEncryptionRequestPacket.
Recommended Posts