views:

39

answers:

1

Hi everyone,

I am not used to C# (I do C++ usually) and try to debug an application that is not mine, at all.

My application tries to read a big line from a TCP socket. Let say around 140 000 characters. And it fails. Let me explain how.

My code is here (inside a loop actually )

            System.IO.Stream inputStream;

            //...

            // Loop code:
            buffer            = new byte[2];
            readByteForLength = inputStream.Read(buffer, 0, 2);

It turns out that Read() may fill in the buffer array correctly up to a point, where it fills it with NULL characters instead of valid values. And it returns 2 as it would in a correct case.

Do you have an idea why such NULL characters?

Is the tcp pacquet still on the network when I try to read more of my data? Is there a limit for inputStream before it behaves wrongly ?

Update: By the way doing so lead to the same kind of issue:

        System.IO.StreamReader sr = new StreamReader(inputStream);
        string s = sr.ReadToEnd();

        File.WriteAllText(@"c:\temp\toto.txt", s);

Actually the toto file stops exactly where I encounter an issue in the first version of my code while it is a little bit longer because the rest of the line is then filled up with NULL characters, nearly up to 400 000!

+1  A: 

The only thing reasonable idea is that you indeed do have zeroes in the incoming data.

Try sniffering on the communication with ethereal.

By the way: allocating RAM for every received data piece may be a wrong practice.

Pavel Radzivilovsky