开发者

How to read data from socket connection - android

开发者 https://www.devze.com 2023-01-20 05:12 出处:网络
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 s开发者_StackOverflow中文版erver responses without wait

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 s开发者_StackOverflow中文版erver 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消