title: Android - Socket Connected even though it cannot
body: <p>Firstly I know what your thinking, it's a strange question and it seems like it cannot be true but here me out on this one...</p>
  
  <p>The project is a project which sends an array of bytes over a socket and also receives data from a socket. I am currently working on protection. I want to make sure that the user knows when the socket can't connect for whatever reason... You can view the connection code below.</p>
  
  <p>So I have an android app which connects to a executable on a Windows Server computer in a office elsewhere in the world. The socket connects via the usual IP address and port number.</p>
  
  <p>I am testing the app using a Android 5.0 (lollipop) phone...</p>
  
  <p>Now enough of the boring stuff:
  If I close the server and turn it off completely and test this on wi-fi then the socket fails - which is correct. It cannot connect, and it will throw a SocketException. Which is fine and is working as expected. Now if I were to turn wi-fi off and use mobile data (I use o2 here in the sunny United Kingdom!) then an issue arises, using the same code, with the server still turned off, I do not throw a SocketException, instead the code simple thinks it has connected on a socket. The connection thread is below. </p>
  
  <pre><code>Thread StartConnection = new Thread()
          {
              @Override
              public void run()
              {
  
                  int dstPort = 10600;
  
                  socket = new Socket();
                  try
                  {
                      ipaddress = InetAddress.getByName(dstName);
                  }
                  catch (UnknownHostException uhe)
                  {
                      uhe.printStackTrace();
                  }
  
                  j = new InetSocketAddress(IPString , dstPort);
  
                  try
                  {
                          socket.setKeepAlive(true);
                          socket.setReuseAddress(true);
                          socket.setTcpNoDelay(true);
                          socket.setSoTimeout(5000);
                          socket.connect(j, 5000);
                          connected = true;
                          outputstream = socket.getOutputStream();
                          DataThread();
                          SocketError = false;
                  }
                  catch (SocketException se)
                  {
                      se.printStackTrace();
                  }
                  catch (IllegalArgumentException iae)
                  {
                      iae.printStackTrace();
                  }
                  catch (IOException ioe)
                  {
                      ioe.printStackTrace();
                  }
                  catch (Exception e)
                  {
                      e.printStackTrace();
                  }
              }
          };
          StartConnection.start();
  </code></pre>
  
  <blockquote>
    <p></p>
  </blockquote>
  
  <pre><code>socket.connect(j, 5000); 
  </code></pre>
  
  <p>works fine no socket errors. No timeouts either. </p>
  
  <pre><code>outputstream = socket.getOutputStream(); 
  </code></pre>
  
  <p>also works. I get a result of "java.net.PlainSocketImpl$PlainSocketOutputStream@48e6f6e" which is also fine. </p>
  
  <p><code>socket.getInputStream()</code> returns "java.net.PlainSocketImpl$PlainSocketInputStream@1ea305a5" </p>