Android: connection failure: ETIMEDOUT (connection timeout) when reconnecting

So, I start a separate thread to use the socket. And when the user clicks the home button, I will close the socket and interrupt the stream. But, after interrupting the stream and after the interrupted user of the old stream, open my application, I will start immediately starting a new stream and connecting to the socket. I get the following error:

    java.net.ConnectException: failed to connect to /12.111.102.100 (port 3001): W/System.err(21440): java.net.ConnectException: failed to connect to /12.111.102.100 (port 3001): connect failed: ETIMEDOUT (Connection timed out)
    at libcore.io.IoBridge.connect(IoBridge.java:124)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
    at java.net.Socket.connect(Socket.java:882)
    at java.net.Socket.connect(Socket.java:825)
    at com.stm.classlife.services.SocketThread.run(SocketThread.java:47)
    Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
    at libcore.io.Posix.connect(Native Method)
    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
    at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
    at libcore.io.IoBridge.connect(IoBridge.java:122)
    ... 5 more

    at libcore.io.IoBridge.connect(IoBridge.java:124)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
    at java.net.Socket.connect(Socket.java:882)
    at java.net.Socket.connect(Socket.java:825)
    at com.stm.classlife.services.SocketThread.run(SocketThread.java:47)
    Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
    at libcore.io.Posix.connect(Native Method)
    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
    at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
    at libcore.io.IoBridge.connect(IoBridge.java:122)
    ... 5 more

I do not understand why I get a connection error, does anyone know how to solve this problem?

Thanks in advance!

+4
source share

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


All Articles