Valhalla Legends Forums Archive | C/C++ Programming | Simple Question

AuthorMessageTime
Spilled[DW]
In my SendPacket Coding Seen here:
[code]
void SendPacket(BYTE PacketID)
{
    char SendBuf[8026];
    int mWord = strlen(buffer);
    SendBuf[0] = 0xFF;
    SendBuf[1] = PacketID;
    *(unsigned short *)(SendBuf + 2) = (WORD) (sizeof(buffer) + 4); // insert packet size (ushort) starting at buffer[2]
    CopyMemory((char *) SendBuf + 2, buffer, mWord);
    mWord = strlen(SendBuf);
    send(wSock, SendBuf, mWord, 1);
}
[/code]

I'm trying to add buffer (which is a char), to SendBuf starting at 4. How would i go about doing this because what im doing again working. Buffer contains 4 0x00 bytes.

Heres my insertbyte coding:

[code]
void SendPacket0x50()
{
    InsertBYTE(0x00);
    InsertBYTE(0x00);
    InsertBYTE(0x00);
    InsertBYTE(0x00);
   
    SendPacket(0x50);
}

void InsertBYTE(BYTE byte)
{
    buffer[bufferpos] = byte;
    bufferpos += 1;
}
[/code]

As you can tell, im new to C++ so plz no flaming.
Thanks in advance!
September 21, 2005, 8:29 PM
rabbit
I use a variable named "buffer_len" that I update in all of my insertion routines...
September 21, 2005, 11:11 PM
UserLoser.
strlen() won't work because when it reaches a null character it'll stop counting.  You need to do what the bunny said and update a private variable everytime you insert something into the buffer to hold the length
September 22, 2005, 1:45 AM
Spilled[DW]
Oh, should have seen that.... guess staring at the code for so long I didn't think about that. Thanks guys!!

Another problem i have ran into is another simple one and didn't think it was worth starting another topic so i'll just ask it here if thats ok.

[code]
*(unsigned short *)(SendBuf + 2) = (WORD) (buffer_len + 4);
[/code]

Am i doing this right? For some reason its not looking right to me... Ideas on that?

Edit: After trial and error I have gotten it working and yes that was the right way Thanks to all that Helped!
September 22, 2005, 6:18 AM
KkBlazekK
Couldn't you use sizeof() or is that for a size of an array or max length?
September 22, 2005, 10:18 PM
K
[quote author=Blaze link=topic=12874.msg128977#msg128977 date=1127427518]
Couldn't you use sizeof() or is that for a size of an array or max length?
[/quote]

sizeof() will only return the size in bytes of an array that has been allocated on the stack.  For any dynamically allocated memory, sizeof() will return the size of the pointer to the memory.
September 22, 2005, 10:23 PM

Search