Mr.Noob
Mr.Noob

Reputation: 1005

Reading from a Network Stream in C#

I just have this issue with reading from a network stream in C#. Since am more of a Java developer I came across this issue.

In java I have this option of knowing the length of the received packet using the following code int length = dataiInputStream.read(rcvPacket);

eventhough the size of the byte array rcvPacket assigned is larger than the amount of elements contained in it. which will allow me to read only the required length of elements so that i do not have elements in the byte array containing zeros.

While i was trying to use a similar thing on C# which was

long len = networkStream.length;

but the docucmentation says that this property is not supported. is there a workaround for this?

Thank you

Upvotes: 0

Views: 336

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503459

The Java code doesn't show you the length of a packet. It shows you the amount of data read within that read call, which could have come from multiple packets.

NetworkStream.Read returns the same information, except using 0 to indicate the end of the stream instead of -1.

Upvotes: 1

Related Questions