![]() |
Header struct I make this struct: char ID[16]; char payloadID; char TTL; char Hops; int payloadlen; But sizeof(mystruct) = 24 ¿? It seems to be 23 bytes long. It MUST to be 23 bytes long. Help me, please. |
sizeof() returns the total storage bytes of the struct including padded bytes for alignment. It is returning 24 because of a byte that was added for alignment. The format of your structure looks fine except payloadID, TTL, Hops, and payloadlen should probably be unsigned. |
Thanks for answer. I have to copy it to a string to send header. I must copy 24 or 23 bytes? (because protocol says must to be 23 bytes). How can I copy? |
You shouldn't have to copy it to a string in order to send it, just pass a pointer to the struct. Only copy/send 23 bytes either way you do it. If you really need to copy it to a string just use memcpy. |
Yes, I copy it with memcpy, but I must specify the size. And, if sizeof says 24bytes, I should copy 24bytes, however I need 23 bytes. Thats my problem. You say I must send a pointer to struct, mmm, if I send a pointer to data on my computer to another PC, I think it will not work. Thanks for answer. |
Quote:
Quote:
i.e.: bytes = send( sock, (char*)&struct, 23, 0 ) |
Thanks a lot. I will use it. |
Failed. I was searching information about this. I found something. A struct can have a diferent size than its elements. it´s happens because there is a byte used to struct. Then I can´t use this solution. I need to copy element by element. If someone has some code to do this, please, tell me. I´m working on that. |
create a small char buffer, memcpy into it and send the buffer. Monitor your serial port and watch to see if the bytes are correct. Look at other client source code and see how they did it and quit re-inventing the wheel. Even better, use another clients source code and build on top of that so we can get more features instead of more base code that we don't need. |
Yes, I read that about wheels before. Bu I need to do that. I try to see another client, but its easiest reinvent the wheel than understand some codes. |
Cerebro: If you could post the code you are having problems with we can tell you how to fix it. Here is an example though: struct GHEADER { unsigned char MsgID[16]; unsigned char FuncID; unsigned char TTL; unsigned char Hops; unsigned long PayloadLen; }; // Assume hdr is a GHEADER struct that has been filled // Method 1 // copy from struct to buffer char buffer[23]; memcpy( buffer, (char*)&hdr, 23 ); // Method 2 // copy struct member by member to buffer char buffer2[23]; memcpy( &buffer[0], hdr.MsgID, 16 ); memcpy( &buffer[16], hdr.FuncID, 1 ); memcpy( &buffer[17], hdr.TTL, 1 ); memcpy( &buffer[18], hdr.Hops, 1 ); memcpy( &buffer[19], hdr.PayloadLen, 4 ); |
Thanks, but I think payload length must be int, because it must to be 4 bytes long. |
On Win32 unsigned int and unsigned long are the same (4 bytes). |
All times are GMT -7. The time now is 11:05 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 ©2011, Crawlability, Inc.
Copyright © 2020 Gnutella Forums.
All Rights Reserved.