Server writes message to client only when Stream Writer Auto Flush given true. Can Anyone tel me Why?

142 Views Asked by At

Here is my server side code that writes the data to client.

         try
         {
            IPHostEntry addr = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress localIP = 
             addr.AddressList.Where(x => 
            x.AddressFamily      ==AddressFamily.InterNetwork).FirstOrDefault();
            //Console.WriteLine(localIP);
            listener = new TcpListener(localIP,2055);
            listener.Start();

            while (true)
            {
                s = listener.AcceptSocket();
                stream = new NetworkStream(s);
                strread = new StreamReader(stream);
                strwrite = new StreamWriter(stream);
                //strwrite.AutoFlush = true;
                strwrite.WriteLine("Hello");
                //    string recvmessage = strread.ReadLine();
                //    Console.WriteLine(recvmessage);
                //    if(string.IsNullOrEmpty(recvmessage))
                //    strwrite.WriteLine("Idealist");
            }


        }

The Server side code writes data to client only when autoflush is given true.Can anyone please explain

1

There are 1 best solutions below

0
On BEST ANSWER

Because it gets flushed automatically, of course. If you look at the Javadoc you'll see that autoflush happens when the data contains a newline. If you don't set this, the data doesn't get flushed until you call flush() yourself, or close the OutputStream or Writer yourself.