James
James

Reputation: 356

Only "odd" sw.Flush()'s working to send streamWriter.WriteLine()'s

I'm not entirely sure what's causing this problem. I have two threaded clients communicating with a server at the same time but for some reason only "odd" sw.Flush()'s are actually sending data to be displayed on the client console.

For example, in the server I am reaching this point:

while (true)
{
       sw.WriteLine("BOO");
       sw.Flush();
       Console.ReadKey();
}

The first time I press a key, "BOO" is sent to the first client window. The second time, "BOO" is sent to the second client window. However for the 3rd and 4th time a key is pressed, nothing is printed and the program continues. It then works again for the 5th and 6th but not for the 7th and 8th etc.

It could be something in my client handling which is pasted below:

 while (true)
            {
                while (sr.ReadLine() != null)
                {
                    Console.WriteLine(readServerResponse(sr));

                }
            }

The readServerResponse method is thus:

public static string readServerResponse(StreamReader sr)
        {
            string responseString = "";
            try
            {
                responseString = sr.ReadLine();

            }
            catch(Exception e)
            {
                Console.WriteLine("Exception: " + e.ToString());
            }
            return responseString;
        }

If anyone could shed any light on this I would be much appreciated.

Upvotes: 0

Views: 63

Answers (1)

Manuel Zelenka
Manuel Zelenka

Reputation: 1634

You throw away messages when you say while(sr.ReadLine() != null). Just store the response in the while in a Variable and remove the ReadLine from your method. Or remove the method and make your try catch Surround the while Block.

Upvotes: 3

Related Questions