C#-StreamReader.ReadLine无法正常工作!
简单地说,我一直在尝试实现C#-StreamReader.ReadLine无法正常工作!,c#,.net,readline,streamreader,bufferedstream,C#,.net,Readline,Streamreader,Bufferedstream,简单地说,我一直在尝试实现BufferedStreamReader在Java中的功能。我打开了一个套接字流,只想以面向行的方式逐行阅读它 我有以下服务器代码 while (continueProcess) { try { StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8);
BufferedStreamReader
在Java中的功能。我打开了一个套接字流,只想以面向行的方式逐行阅读它
我有以下服务器代码
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());
}
}
以及以下客户端代码:
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();
}
服务器只读取第一行(login>user,pass
),然后ReadLine
返回null
在Java的
BufferedStreamReader
中,实现这种面向行的阅读器的最简单方法是什么服务器代码中的while设置为每个连接只读取一行。您将需要另一个,而在尝试读取所有发送的行。我认为,一旦在客户端设置了该流,它将发送所有数据。然后在服务器端,您的流实际上只从该特定流中读取一行。典型的行读取器类似于:
using(StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8)) {
string line;
while((line = reader.ReadLine()) != null) {
// do something with line
}
}
(请注意使用
以确保我们Dispose()
即使出现错误,也会执行该操作,以及循环)
如果需要,可以使用迭代器块来抽象(关注点分离):
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;
}
}
}
现在,我们的处理代码不需要担心如何读取行—只要给定一系列行,就可以对它们进行处理
请注意,使用(Dispose()
)的也适用于TcpClient
;你应该养成检查IDisposable
的习惯;例如(仍然包括您的错误日志记录):
试过这个,得到了
找不到类型或命名空间名称“Stream”(是否缺少using指令或程序集引用?)
找不到类型或命名空间名称“StreamReader”(是否缺少using指令或程序集引用?)
找不到类型或命名空间名称“StreamReader”(是否缺少using指令或程序集引用?)
“System.Net.Sockets.Socket”不包含“GetStream”的定义
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是一个套接字。
对于某些重分区,TcpClient类在我的电脑上不再正常工作,
但是Socket类工作得很好
UTF-8是搁浅的StreamReader/Writer编码请在此处处理您的对象(StreamReader和StreamWriter)。否则,您可能会遇到意想不到的问题。只需在它们周围放置一个using块(如using(StreamWriter writer=…){}),您就会知道它与问题无关-只是一个建议(continueProcess)…客户端:…线程.睡眠(10000);writer.WriteLine(“打印>测试,一,二”);我插入了线程.睡眠(10000)到我的客户端代码,服务器开始抛出异常。客户端套接字将一直打开,直到客户端退出,并且由于客户端将在其整个生命周期内发送数据,因此此代码似乎会抛出错误。你不这样认为吗?在没有看到新代码或新异常的情况下,我不确定可能是什么问题。当我使用StreamReader进行readin时g从套接字中,我发现客户端程序正在冻结,在ReadLine()方法中花费了无限长的时间,最终无法读取。@MasudRahman这通常意味着:服务器没有发送完整的行(即,您要求它等待服务器没有理由发送给您的内容),或者服务器忘记刷新输出缓冲区。您可以添加读取超时,但最终无法使服务器发送数据。当然,您可以自己进行读取和缓冲,但这只会改变问题的性质—然后,您的工作就是确定一段时间后未发送完整的行。
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());
}
}
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
}