Android connecting server, how do I stop the socket after a certain amount of time?
I'm developing a personal android application used at home for my own functionality.
I am trying to make it so that if the Server at home is either down or non-functional, it will disconnect after 3 seconds.
Here's my code:
Socket socket = new Socket("10.0.2.2", 26753); socket.setSoTimeout(3000); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); out.println("y" + e.getText().toString()); out.println("u" + a.getText().toString()); out.println("EOF"); socket.close();
Ignore the EOF at the end. That is there so that my server knows when to close the socket
So I've put setSoTimeout to 3000 (3 seconds), but android application doesn't respond. Can I put it in a thread, how would I do it?
You may want to try using a connect timeout:
Socket socket = new Socket(); socket.setSoTimeout(3000); socket.connect(new InetSocketAddress("10.0.2.2", 26753), 3000);