C# - StreamReader.ReadLine does not work properly!

前端 未结 4 1158
南方客
南方客 2020-12-09 19:37

Simply I have been trying to implement what BufferedStreamReader does in Java. I have a socket stream open and just want to read it in a line-oriented fashion -

4条回答
  •  清歌不尽
    2020-12-09 20:21

        public string READS()
        {
            byte[] buf = new byte[CLI.Available];//set buffer
            CLI.Receive(buf);//read bytes from stream
            string line = UTF8Encoding.UTF8.GetString(buf);//get string from bytes
            return line;//return string from bytes
        }
        public void WRITES(string text)
        {
            byte[] buf = UTF8Encoding.UTF8.GetBytes(text);//get bytes of text
            CLI.Send(buf);//send bytes
        }
    

    CLI is a socket. for some rezone the TcpClient class doesn't work right on my pc any more, but the Socket class works just fine.

    UTF-8 is the stranded StreamReader / Writer Encoding

提交回复
热议问题