C# - StreamReader.ReadLine不能正常工作! [英] C# - StreamReader.ReadLine does not work properly!
问题描述
只要我一直在努力实现的东西 BufferedStreamReader
确实在Java中。我有一个套接字流开,只是想读它在一个面向行的方式 - 线,由线
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 - line by line.
我有以下的服务器 - code。
I have the following server-code.
while (continueProcess)
{
try
{
StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8);
string command = reader.ReadLine();
if (command == null)
break;
OnClientExecute(command);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
和下面的客户端 - code:
And the following client-code:
TcpClient tcpClient = new TcpClient();
try
{
tcpClient.Connect("localhost", serverPort);
StreamWriter writer = new StreamWriter(tcpClient.GetStream(), Encoding.UTF8);
writer.AutoFlush = true;
writer.WriteLine("login>user,pass");
writer.WriteLine("print>param1,param2,param3");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
tcpClient.Close();
}
服务器只读取第一行(登录>用户,通过
),然后的ReadLine
返回null!
The server reads only the very first line (login>user,pass
) and then ReadLine
returns null!
什么是实现这一目标的面向行的读者,因为它是在Java中的 BufferedStreamReader
的最简单的方法? :■
What's the easiest way of achieving this line-oriented reader as it is in Java's BufferedStreamReader
? :s
推荐答案
一个典型的行阅读器是一样的东西:
A typical line-reader is something like:
using(StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8)) {
string line;
while((line = reader.ReadLine()) != null) {
// do something with line
}
}
(注意使用
,以确保我们的Dispose()
它,即使我们得到一个错误,循环)
(note the using
to ensure we Dispose()
it even if we get an error, and the loop)
如果你愿意,你可以摘要(关注点分离)与迭代器块:
If you want, you could abstract this (separation of concerns) with an iterator block:
static IEnumerable<string> ReadLines(Stream source, Encoding encoding) {
using(StreamReader reader = new StreamReader(source, encoding)) {
string line;
while((line = reader.ReadLine()) != null) {
yield return line;
}
}
}
(注意,我们已经转移到这一个功能,去掉了做什么,以收益回报,这将创建一个迭代器(一个懒洋洋地迭代,非缓冲状态机)替换它
(note we've moved this into a function and removed the "do something", replacing it with "yield return", which creates an iterator (a lazily iterated, non-buffering state machine)
我们会再消费这是简单的:
We would then consume this as simply as:
foreach(string line in ReadLines(Socket.GetStream(), Encoding.UTF8)) {
// do something with line
}
现在我们处理code并不需要担心的如何的读线 - 简单的给的行的顺序,做一些与他们
Now our processing code doesn't need to worry about how to read lines - simply given a sequence of lines, do something with them.
注意使用
(的Dispose()
)适用于的TcpClient
过;你应该检查的IDisposable
的习惯;例如(仍包括您的错误记录):
Note that the using
(Dispose()
) applies to TcpClient
too; you should make a habit of checking for IDisposable
; for example (still including your error-logging):
using(TcpClient tcpClient = new TcpClient()) {
try {
tcpClient.Connect("localhost", serverPort);
StreamWriter writer = new StreamWriter(tcpClient.GetStream(), Encoding.UTF8);
writer.AutoFlush = true;
writer.WriteLine("login>user,pass");
writer.WriteLine("print>param1,param2,param3");
} catch (Exception ex) {
Console.Error.WriteLine(ex.ToString());
}
}
这篇关于C# - StreamReader.ReadLine不能正常工作!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!