![]() |
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. |
All times are GMT -7. The time now is 07:25 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.