简单地说,我一直在试图实现BufferedStreamReader
在Java中的作用。我有一个套接字流打开,只是想以面向行的方式逐行读取它。C# - StreamReader.ReadLine无法正常工作!
我有以下服务器代码。
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
返回空!
在Java的BufferedStreamReader
中,实现这种面向行的阅读器的最简单方法是什么? :s
请,处置您的物体在这里(的的StreamReader和StreamWriter)。否则,您可能会遇到意外问题。简单地在它们周围放置一个使用块(如使用(StreamWriter writer = ...){}),并且你会好的 – configurator 2009-09-20 04:15:42
不是它与问题相关 - 只是一个建议。 – configurator 2009-09-20 04:16:12
很棒; )感谢 – Aleyna 2009-09-20 04:31:20