Other examples showed you how to trim a string, but it would be better not to start with "bad" data.
I assume you have code like this:
// Bad code byte[] data = new byte[8192]; stream.Read(data, 0, data.Length); string text = Encoding.ASCII.GetString(data);
This ignores the return value of Stream.Read . Instead, your code should look something like this:
// Better code byte[] data = new byte[8192]; int bytesRead = stream.Read(data, 0, data.Length); string text = Encoding.ASCII.GetString(data, 0, bytesRead);
Please note that you should also check if the stream was closed, and do not assume that you can read all the data in one Read call or one call to Write on the other end corresponds to one Read call.
Of course, it is entirely possible that this is not at all the case, but you really have to check if the other end is trying these extra "null" bytes at the end of the data. It seems unlikely to me.
source share