Android Connection Timeout

I am trying to implement tcp client application on Android. When I try to connect to my C ++ server, the socket expires when I try to connect to the server.

My code is:

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, ERROR1 (socket is null) occurs, then ERROR2 (connection time) occurs. The server is working fine, I tested it with different clients. I have use-permission, so this should not be a problem.

EDIT: stack in 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 
+6
source share
1 answer

"Connection timeout" is a network connection problem. For example, there may be a firewall. This is not a programming problem, and you cannot solve it in code.

+11
source

Source: https://habr.com/ru/post/969410/


All Articles