请看这里的例子。
为了能够保持连接活动并向服务器发送多个客户端消息,我将代码更改为:
// Send the message to the server
OutputStream os = Client.socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
InputStream is = Client.socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
for (int i = 0; i < 10; i++) {
String sendMessage = i + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : " + sendMessage);
String message = br.readLine();
System.out.println("Message received from the server : " + message);
}
我不明白为什么这只适用于第一条消息。
有人可以解释一下吗?
更新:
系统输出:
服务器端:
`Server Started and listening to the port 25000`
`Message received from client is 0`
`Message sent to the client is 0`
客户端:
`Message sent to the server : 0`
`Message received from the server : 0`
`Message sent to the server : 1`