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