androidsocketstcpnexus-5

TCP connection refused on Nexus 5X


I have a simple application where I open a TCP socket connection to a server and then send some data over it. It works fine but I am getting the following exception on Nexus 5X devices. Since I am able to connect to the server with other devices its not a issue on server side.

09:00:05.118+03:00 D/com.testing.android.util.Util getCurrentGatewayIP: 192.168.1.1
09:00:05.120+03:00 D/com.testing.android.util.TCPClient Connect()
09:00:05.121+03:00 D/com.testing.android.util.TCPClient openSocket
09:00:17.162+03:00 E/com.testing.android.util.TCPClient sendInfo
09:00:17.163+03:00 E/com.testing.android.util.TCPClient java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
    at java.net.Socket.connect(Socket.java:586)
    at com.testing.android.util.TCPClient.openSocket(TCPClient.java:208)
    at com.testing.android.util.TCPClient.connect(TCPClient.java:53)

    at android.os.AsyncTask$2.call(AsyncTask.java:304)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)

UPDATE:

It looks like Nexus 5X blocks access to the server (which is part of LAN and connects to the phone through wifi). This network does not have internet connectivity. On other android devices you will get a notification "Wifi has no Internet access - This network has no Internet access. Stay connected? (Yes/No)" but you will still be able to communicate within the LAN. On Nexus unless user has accepted that they want to stay connected to the network all communication is blocked.


Solution

  • Right now there is no programatic solution for this problem. The user has to allow the network manually. Since this is a security feature for detection of Captive Portals only system apps can disable this.

    This post describes few more details