I'm writing an TCP client in Android that sends data to a Java server. I'm now adding validation for when the server's not available and I got stuck on what to do when the server is killed suddenly (I'm actually exitting the server with JFrame.EXIT_ON_CLOSE
since is a test server).
The issue is that when I close the server, the the client doesn't throws any exception whenever I write to the OutputStream
nor when I read from the InputStream
.
// This is inside a try block, catching IOException, and finally block closes all streams
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (mRun) {
String response = in.readLine();
Message message = Message.obtain();
if (response != null && message != null) {
message.obj = response;
mHandler.sendMessage(message);
}
}
Also, when I close the server, the system keeps logging about 10-20 of the following logs per second, or maybe more:
dalvikvm﹕ GC_FOR_ALLOC freed 499K, 6% free 9377K/9920K, paused 13ms, total 13ms
ALSO I have another thread that attempts to write to the Sockets
's OutputStream
by using PrintWriter
:
if(out != null && !out.checkError()) {
Log.d(TAG, "Writing to output stream...");
//...
And it seems to keep executing, which means checkError()
is returning false
I'm new to Socket
programming, so an explanation of why should I do what you tell me to do would be nice, thanks in advance.