1
下面的代碼工作,服務器獲取com
字符串,然後連接關閉,如果我取消註釋bw.flush()
及其下一行,服務器不會收到com字符串並且它只是在br.readLine()
上等待響應(因爲服務器沒有收到com字符串,所以不會得到響應),如果我移動bw.close()
並將其置於bw.flush()
位置,則br.readLine()
將引發Socket關閉異常。套接字BufferedWriter.flush()不發送數據
public static void setupClient(String[] args) throws UnknownHostException, IOException{
Socket client = new Socket(InetAddress.getLocalHost().getHostName(), Server.DEFAULT_COMMAND_PORT);
BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
BufferedReader br =new BufferedReader(new InputStreamReader(client.getInputStream()));
String com=args[0]+(args.length==2?args[1]:"");
bw.write(com);
// bw.flush();
// System.out.println("response:"+br.readLine());
bw.close();
client.close();
}