❔ 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
Include the size of the packet within the header.
Packet Type (4 bytes) | Payload Length (4 bytes / 8 bytes) | Payload (X bytes)
I am using TcpClient and NetworkStream, i can do this?
i havent come across this yet, ill take a look
Instead of sending just the data, send the type of data AND the size of data
Do i need to use sockets for this?
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
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
Exactly.
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?
It's usually how packets are made
having said that, never actually worked with packets so i wouldnt know
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
sounds doable this, last thing. am i imagining that ive seen a PacketBuilder class or similar
Although headers can contain much more information, such as when the packet was sent, the client version, and so forth.
affirmative, its all coming back to me now, i believe we learnt about packets briefly last year in school
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.