简体   繁体   English

从NetworkStream读取到定界符

[英]Read from NetworkStream to delimiter

what is the best way to read from NetworkStream to some delimiter (for example "\\n") 从NetworkStream读取某些定界符(例如“ \\ 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. 问题是,从客户端发送诸如“某些文本\\ n在换行符上继续”之类的内容,但我想首先对“某些文本”进行回答,然后接受下一行并发送响应。

If you just want to read a line then use StreamReader on your NetworkStream and call its ReadLine method: 如果只想读取一行,则在NetworkStream上使用StreamReader并调用其ReadLine方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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