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

StackOverflow https://stackoverflow.com/questions/21728002

  •  10-10-2022
  •  | 
  •  

Pregunta

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

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top