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
  •  | 
  •  

سؤال

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

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top