samedi 23 juillet 2016

Reading from socket blocks

I'm trying to read a response from a server. I have two methods. The first one doesn't block but the second one does. I don't understand why the difference..

Method 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;
}

Method 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;
}

Aucun commentaire:

Enregistrer un commentaire