Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - socket timeout while connecting

I'm trying to implement a tcp client app on Android. When I try to connect to my C++ server, the socket times out while trying to connect to the server.

My code:

new Thread(new ClientThread()).start();
try
{
  PrintWriter out = new PrintWriter(new BufferedWriter(
    new OutputStreamWriter(socket.getOutputStream())), true);
  out.println("Test message.");
}
catch (Exception e)
{
  // ERROR1
  e.printStackTrace();
}

...

class ClientThread implements Runnable
{
  @Override
  public void run()
  {
    try
    {
      InetAddress serverAddr = InetAddress.getByName("192.168.1.116");
      socket = new Socket(serverAddr, 9000);
    }
    catch (Exception e)
    {
      // ERROR2
      e.printStackTrace();
    }
  }
}

First, the ERROR1 occurs (socket is null), then the ERROR2 occurs (connection time out). The server is working fine, I have tested it with different clients. I have "uses-permission" so it shouldn't be a problem.

EDIT: stack at ERROR2:

05-17 02:26:50.789: W/System.err(26625): java.net.ConnectException: failed to connect to /192.168.1.116 (port 9000): connect failed: ETIMEDOUT (Connection timed out)
05-17 02:26:50.789: W/System.err(26625):    at libcore.io.IoBridge.connect(IoBridge.java:114)
05-17 02:26:50.789: W/System.err(26625):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-17 02:26:50.789: W/System.err(26625):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
05-17 02:26:50.789: W/System.err(26625):    at java.net.Socket.startupSocket(Socket.java:566)
05-17 02:26:50.789: W/System.err(26625):    at java.net.Socket.<init>(Socket.java:225)
05-17 02:26:50.789: W/System.err(26625):    at cz.gclient.gardenclient.MainActivity$ClientThread.run(MainActivity.java:153)
05-17 02:26:50.789: W/System.err(26625):    at java.lang.Thread.run(Thread.java:841)
05-17 02:26:50.789: W/System.err(26625): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
05-17 02:26:50.789: W/System.err(26625):    at libcore.io.Posix.connect(Native Method)
05-17 02:26:50.789: W/System.err(26625):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
05-17 02:26:50.789: W/System.err(26625):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-17 02:26:50.789: W/System.err(26625):    at libcore.io.IoBridge.connect(IoBridge.java:112)
05-17 02:26:50.789: W/System.err(26625):    ... 6 more
like image 405
0x0000eWan Avatar asked May 16 '14 22:05

0x0000eWan


People also ask

How do I fix connection timeout on Android?

Restart your device. Open your Settings app and tap Network & internet or Connections. Depending on your device, these options may be different. Turn Wi-Fi off and mobile data on, and check if there's a difference. If not, turn mobile data off and Wi-Fi on and check again.

How do I fix socket timeout error?

Using try/catch/finally If you are a developer, so you can surround the socket connection part of your code in a try/catch/finally and handle the error in the catch. You might try connecting a second time, or try connecting to another possible socket, or simply exit the program cleanly.

What causes socket timeouts?

Socket timeouts can occur when attempting to connect to a remote server, or during communication, especially long-lived ones. They can be caused by any connectivity problem on the network, such as: A network partition preventing the two machines from communicating. The remote machine crashing.

What is connection timeout and socket timeout?

connection timeout — a time period in which a client should establish a connection with a server. socket timeout — a maximum time of inactivity between two data packets when exchanging data with a server.


1 Answers

'Connection time out' is a network connectivity problem. There may be a firewall in the way for example. It isn't a programming problem and you can't solve it in code.

like image 129
user207421 Avatar answered Oct 26 '22 08:10

user207421