I have a byte array that I am reading in NetworkStream. The first two bytes indicate the length of the subsequent packet, and then the packet is read into an array of bytes of this length. The data I need to read from the NetworkStream / byte array has several lines, that is, variable-length data ending with new lowercase characters, and some fixed-width fields, such as bytes and long. So something like this:
I know (and mean) the data packet format that occurs, and what I need to do is read the "string" for each string value, but read a fixed number of bytes for bytes and long. So far, my proposed solution has been to use a while to read bytes into the temp byte array until a newline appears. Then convert the bytes to a string. This seems awkward to me, but I see no other obvious way. I understand that I can use StreamReader.ReadLine() , but this will be connected to another stream, and I already have NetworkStream . But if this is the best solution, I will do it.
Another option that I considered is for my backend command to write bytes or two for these line lengths so that I can read the length and then read the line based on the specified length.
So, as you can see, I have some options for how to do this, and I would like you to talk about what you think is the best way to do this. Here is the code that I have right now for reading in the entire package as a string. The next step is to break down the various fields of the package and do the actual programming work that needs to be done, creating objects, updating the user interface, etc. Based on the data in the package.
string line = null; while (stream.DataAvailable) {
jxpx777
source share