【发布时间】:2014-03-07 07:58:33
【问题描述】:
我正在关注 Oracle 的 java sockets 教程:docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
try (
// ...
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
String inputLine, outputLine;
// Initiate conversation with client
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye."))
break;
}
本教程指出“readLine 方法会一直等待,直到客户端通过向其输出流(服务器的输入流)写入内容来做出响应”。
但是服务器的输入流“in”是使用客户端的输入流初始化的,而不是它的输出流。
不应该用clientSocket.getOutputStream()来初始化“in”,来监听客户端的OUTput吗?
【问题讨论】: