When you deal with network communications you must define a "protocol" to define what your "Message" is because network connections are Stream based not Message based.
So in the protocal is defined as the following
Description -> |dataIdentifier|name length|message length| name | message |
Size in bytes -> | 4 | 4 | 4 |name length|message length|
A int
will always be a System.Int32
and a System.Int32
will always take 4 bytes to store (divide 32 by 8 and you get 4).
Here is another line showing the data types of each column, maybe that will help you
Description -> |dataIdentifier|name length|message length| name | message |
Size in bytes -> | 4 | 4 | 4 |name length|message length|
Data Type -> | int | int | int | string | string |
So, now why do we skip 4 bytes in the bit converter.
Lets put the schema back up but this time I will but numbers down indicating the number of bytes
Now the last two are "special" their length is not a fixed length like the first 3, what they do is take the value from a previous column and that is how many bytes are read in.
Description -> |dataIdentifier|name length|message length| name | message |
Size in bytes -> | 4 | 4 | 4 | name length | message length |
Bytes -> |0 1 2 3 | 4 5 6 7 | 8 9 10 11 | 12 through (12 + name length) | (12 + name length) + 1 through ((12 + name length) + 1 + message length) |
So you can see to read dataIdentifier
we start at index 0 and read 4 bytes (which is how many BitConverter.ToInt32
will read). Then when we want to read nameLength
we need to start at index 4 then read another 4 bytes, that is why BitConverter.ToInt32
is passed 4 (and messageLength
will be passed 8)
If there is anything not clear please say so in a comment and I will elaborate.