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?

Answers


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);

Need Your Help

Creating icon fonts with vector software (i.e. inkscape) and fontforge?

fonts svg true-type-fonts inkscape fontforge

Through getting some answers here and some research, I've come across a new approach to implementing icons. Rather than as images or css background, it seems you can approach icons as a font.

How to scroll elements on canvas in different states and in different containers and not move others

flex scroll state move dynamic

I generate panels dynamically and put them in other states in that way so in one state you have a list of panels on the left and one big panel on the right for example in one state and when you cli...