0

I'm studying one chat application from the tutorial at http://www.daveoncsharp.com/2009/08/csharp-chat-application-over-asynchronous-udp-sockets-part-1. But i have a issue, anyone can explain for me that in "Packet class", why we know "size in bytes" of "dataIdentifier" is 4 , "name length" is 4, i see description at top of the packet class but i don't know why. And these:

this.dataIdentifier = (DataIdentifier)BitConverter.ToInt32(dataStream, 0); // We will convert from index 0 of dataStream, but how it know to end ???

int nameLength = BitConverter.ToInt32(dataStream, 4); // Why we know it begin from 4?

Thank you very much and sorry for my English.

Thanh Le
  • 235
  • 1
  • 3
  • 15

1 Answers1

0

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.

Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • Thank you very much, I have understand the problem! – Thanh Le Sep 16 '13 at 17:13
  • excuse me, please let me ask is the enum type and int type are both takes 4 bytes to store? – Thanh Le Sep 16 '13 at 17:26
  • It all depends on how the enum was defined, by default the enum is stored as an `int`, but take a look at [the documentation](http://msdn.microsoft.com/en-us/library/sbbt4032.aspx) "*Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of enumeration elements is int. To declare an enum of another integral type, such as byte, use a colon after the identifier followed by the type...*" so if they are using the default if `int` and a `int` takes 4 bytes to store, then yes a enum takes 4 bytes (if they used an int as the backing) – Scott Chamberlain Sep 16 '13 at 17:48
  • Thanks @Scott, I have a further question is how do we know that the first 4 bytes of "dataIdentifier", 4 bytes second of "nameLength", and next of "msgLength"? Why the first 4 bytes not of "nameLength"? – Thanh Le Sep 18 '13 at 12:36