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
ですか? :s