我正嘗試通過Java中的網絡通過新行發送文本。例如:通過網絡發送帶有新行的文本
「你好\ n這是一個新的生產線」
這裏是我的代碼,我使用的客戶端:
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
/*connect to server here*/
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null) {
System.out.print("Server: " + fromServer);
if (fromServer.equals("Bye.")) {
break;
}
fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.print("Client: " + fromUser);
out.println(fromUser);
}
}
out.close();
in.close();
stdIn.close();
kkSocket.close();
,這裏是我的代碼使用服務器:
/*Connect to server here*/
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;
Protocol kkp = new Protocol();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye.")) break;
}
但它只返回第一行。
是否有人能夠指出我在正確的方向,所以我可以使用新的線?
'而((fromServer = in.readLine())= null)':你的客戶似乎期望從服務器上繼續下去。你的服務器發送了什麼? – 2013-03-21 07:54:43