Normally in TCP socket programming, if the remote end closes the
connection, the local program is notified by a return value (in C) or
an exception (in Java). But in Android, if I terminate the remote end
of a socket connection while transmitting data, no exception is
thrown, and no error reported. Is this a bug, or is there something
about socket programming in Android I'm missing?
Here's some sample Android code to demonstrate. If you run
$ netcat -l -p 1234
It will receive the "test" transmissions, but if you kill netcat, the
Android app keeps running without printing a stack trace, indicating
no exception has been thrown.
Socket socket = null;
OutputStream os = null;
try {
socket = new Socket("10.0.2.2", 1234);
os = socket.getOutputStream();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
Log.i("test", "test");
os.write("test\n".getBytes());
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
And a standard Java app. If you kill netcat while this is running,
you'll receive the expected SocketException.
public class SocketTest {
private static Socket mSocket;
private static OutputStream mOS;
public static void main(String[] args) throws IOException {
try {
mSocket = new Socket("127.0.0.1", 1234);
mOS = mSocket.getOutputStream();
} catch (UnknownHostException e) {
e.printStackTrace();
}
while (true){
System.out.println("test");
mOS.write("test\n".getBytes());
mOS.flush();
}
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---