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

Why Resultset trimmed?

java jdbc resultset jtds mssql-jdbc

When referring to the stored procedure client get two Resultset. With the first all is well, but the second consists of 20 rows, client developers claim that the procedure returns about 1000.

Find a specific class in a file using reflection?

c# reflection find

I googled and someone found the answer and linked but it was dead. How can i find a specific class in a specific file? The poster i found ask that question and found how to do it in a namespace but i

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.