views:

48

answers:

1

I've got TCP client app which successfully negotiates connection to server and receives buffered output, but what I need to know is how to read server responses without waiting to buffer to fill out or server to end connection.
In this loop:

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
   ...some code here...         
}

mine app just freezes.
How to read just one line or empty string if buffer is empty and continuing to execute a program?
Is it possible to timeout this reading, to give server some time to respond?

A: 

The underlying protocol should provide some kind of synchronisation, like HTTP's Content-Length or \n\r.

Otherwise, you'll get blocked even if your program reads byte by byte.

Or you could use a non-blocking socket.

mrrtnn
I don't have that kind of trouble with win apps. I can easily read server answers line by line or get notified about no answer with 500 ms timeout without interface freeze. Is this not possible with standard android libraries?
Ravien
ah, UI freeze. That's different. Everything related to UI in Android runs in the "main" thread. I'm afraid you will have to read the socket in a separate thread. Please have a look to http://android-developers.blogspot.com/2009/05/painless-threading.html . This may help as well http://stackoverflow.com/questions/3031280/threading-in-android
mrrtnn