Get rid of the if(theStream.DataAvailable)
. You cannot check if data is available that way since if you have already received it, it is not available. While the ReadLine
function only returns one line to you, it may read much more than one line.
So here's what happens:
All data is sent.
Data is available, you call
ReadLine
. It reads all the data and returns one line to you.No data is available now, since it has already been read from the connection.
There are other problems with that check too. If it's trying to avoid calling ReadLine
if a line isn't available, it won't do that. Some data being available doesn't mean a whole line is. (Imagine if the other end maliciously sends just a single X
byte.)