C
C#2y ago
Boggo

❔ Sizing of Byte Arrays

So im sending byte arrays over a network stream, the byte arrays represent objects of various types. I am wondering how best to assign a size to the byte array when recieving it, should it be done dynamically in each case somehow? or does it not matter if the end of the byte array is empty?
15 Replies
Buddy
Buddy2y ago
Include the size of the packet within the header. Packet Type (4 bytes) | Payload Length (4 bytes / 8 bytes) | Payload (X bytes)
Boggo
BoggoOP2y ago
I am using TcpClient and NetworkStream, i can do this? i havent come across this yet, ill take a look
Buddy
Buddy2y ago
Instead of sending just the data, send the type of data AND the size of data
Boggo
BoggoOP2y ago
Do i need to use sockets for this?
Buddy
Buddy2y ago
So when you receive the data. First parse the header, which is the type of packet and then the size of the payload. Second receive the data until all bytes has been fully read. Third, deserialize the payload and then handle it. TcpClient is just a wrapper around Socket
Boggo
BoggoOP2y ago
oh i see so when i receive, i know to read 4 bytes, to get the type then read the next 4 or 8 to get the length then read the length
Buddy
Buddy2y ago
Exactly.
Boggo
BoggoOP2y ago
so this structure here is almost metaphorical as im not exactly building a packet with a header as such more so implementing the theory of it?
Buddy
Buddy2y ago
It's usually how packets are made
Boggo
BoggoOP2y ago
having said that, never actually worked with packets so i wouldnt know
Buddy
Buddy2y ago
Packet type, Payload length and then Payload itself. That is the basic structure of a simple packet system. You just have to figure a way to deserialize the payload after that, and to handle it. Then it's pretty much done
Boggo
BoggoOP2y ago
sounds doable this, last thing. am i imagining that ive seen a PacketBuilder class or similar
Buddy
Buddy2y ago
Although headers can contain much more information, such as when the packet was sent, the client version, and so forth.
Boggo
BoggoOP2y ago
affirmative, its all coming back to me now, i believe we learnt about packets briefly last year in school
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server