简体   繁体   中英

Read from NetworkStream to delimiter

what is the best way to read from NetworkStream to some delimiter (for example "\\n")

I have following code:

        NetworkStream clientStream = tcpClient.GetStream();
        var message = new byte[4096];

        while (true)
        {
            int bytesRead = 0;

            try
            {
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch
            {
                // Exception
            }
            Response(message);
        }

Problem is, that from client sends something like "Some text\\n continues on newline" but I would like to answer first on "Some text", then accept next line and send response.

If you just want to read a line then use StreamReader on your NetworkStream and call its ReadLine method:

NetworkStream strm = client.GetStream();
StreamReader reader = new StreamReader(strm);
String line = reader.ReadLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM