我試圖從服務器讀取響應。我有兩種方法。第一個不阻擋,但第二個阻止。我不理解爲什麼差..從套接字塊讀取
方法1:
public static String lireReponse() throws IOException, NullPointerException {
// recepteurReponses is a BufferedInputStream
String reponse = "";
byte[] flux = new byte[1024];
int i = recepteurReponses.read(flux);
reponse += new String(flux, 0, i);
return reponse;
}
方法2:
public static String lireDonnees() throws IOException, NullPointerException {
String reponse = "";
byte[] flux = new byte[1024];
int i = -1;
while ((i = recepteurReponses.read(flux)) != -1)
reponse += new String(flux, 0, i);
return reponse;
}
'recepteurReponses'是否關閉過? –
什麼使我確信第一種方法不會阻止是客戶端得到答案,我可以在控制檯中打印它。並且當客戶端向服務器發送CLOSE請求時,recepteurReponses被關閉 – Mouley