shine_joseph shine_joseph - 3 months ago 15
iOS Question

read end of response in java readLine()

try {
input = new BufferedReader(new InputStreamReader(
mSocket.getInputStream()));
while (!Thread.currentThread().isInterrupted()) {
String messageStr = null;
messageStr = input.readLine();
if (messageStr != null) {
updateMessages(messageStr, false);
} else {
break;
}
}
input.close();
} catch (IOException e) {
Log.e(CLIENT_TAG, "Server loop error: ", e);
}


I am using the above code in a thread for receiving responses from socket connection.

In Android it works correctly as I used
out.println()
for sending data, but when the device is connected to ios and starts to receive data it cannot identify the end and is only received when the connection is closed. Is there any alternative methods other than
readLine()
and how to use in the above code.

Answer

This would work better.

try 
{
    input = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
    String messageStr = "";
    while (!Thread.currentThread().isInterrupted() && (messageStr = input.readLine()) != null) 
    {
        updateMessages(messageStr, false);
    }
    input.close();
} catch (Exception e) {
     Log.e(CLIENT_TAG, "Server loop error: ", e);
}
Comments