Gnutella Forums

Gnutella Forums (https://www.gnutellaforums.com/)
-   General Gnutella Development Discussion (https://www.gnutellaforums.com/general-gnutella-development-discussion/)
-   -   Header struct (https://www.gnutellaforums.com/general-gnutella-development-discussion/10775-header-struct.html)

Cerebro April 24th, 2002 08:36 AM

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.

tshdos April 24th, 2002 10:33 AM

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.

Cerebro April 25th, 2002 04:26 AM

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?

tshdos April 25th, 2002 06:47 AM

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.

Cerebro April 25th, 2002 07:51 AM

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.

tshdos April 25th, 2002 08:12 AM

Quote:

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.
memcpy( string, &struct, 23 )

Quote:

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.
Obviously that would not work. I am saying to pass a pointer to the struct to your send function.

i.e.:
bytes = send( sock, (char*)&struct, 23, 0 )

Cerebro April 25th, 2002 08:35 AM

Thanks a lot. I will use it.

Cerebro April 30th, 2002 04:58 AM

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.

Unregistered April 30th, 2002 05:49 AM

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.

Cerebro April 30th, 2002 09:16 AM

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.

tshdos April 30th, 2002 04:43 PM

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 );

Cerebro May 2nd, 2002 05:11 AM

Thanks, but I think payload length must be int, because it must to be 4 bytes long.

tshdos May 2nd, 2002 06:13 AM

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.