title: Socket Connection always returns true even if Port is not open (Port Scan)
body: <h2>Question:</h2>
  
  <p>I have this <strong>FOR Statement</strong>, that calls a Method with different IP Addresses <em>(ranging from 10.0.0.1 to 10.0.0.255 because I don't have an IP scanner)</em>. This Method is a Future Callable and returns a Boolean true or false.
  The function of the Method is to basically make a <strong>Socket</strong>, connect to this Socket with given IP &amp; port + a timeout, and return true or false based on if given IP address, has the port open or not.</p>
  
  <p><strong>My Plan</strong>:
  I'm trying to Check if a device connected to the same WIFI network has an open port (SSH port) so I can SSH into this device without knowing it's IP-Address. My idea was to get the private IP-Address of the Android device, check if its equal to common private IP-Address (for example 10.0.0.*) and scan it's IP-Range (from 10.0.0.1-255) for the open SSH Port (22).</p>
  
  <hr>
  
  <p><strong>ToDo:</strong></p>
  
  <ol>
  <li>Get IP from Android device (<strong>Checked</strong>)</li>
  <li>Filter it, if its a private IP starting with "10." &amp;&amp; "10.0.0." OR "192." &amp;&amp; "192.168." and so on (<strong>Checked</strong>)</li>
  <li>Check if the Port 22 is open or not on the Range "10.0.0.1-255" or "192.168.1.1-255". (<strong>Current</strong>)</li>
  </ol>
  
  <p>I used code from here for the Port Scanner: <a href="https://stackoverflow.com/q/11547082/5704196">Socket Port Scanner</a> In this question the Future Method returned true or false if the port on a fixed IP-Address was open or not. The result of this Method then was added to a Future Boolean List Array. Then queried, the User could see how many ports were open on this fixed IP-Address.</p>
  
  <p>I modified it so the Method was scanning a number of IP-Addresses for a fixed port, and then adding the IP-Addresses, were the SSH Port was open, to the Future Boolean List Array. This works except <code>socket.connect(new InetSocketAddress(ip, port), timeout);</code> always returns true. I tried looking at its code as to why it does that, but I found no answers. If you have any Idea how I could have the Socket return false, when the given port on a IP address is not open, then please let me know. Thanks in Advance.</p>
  
  <h2>Code:</h2>
  
  <p>Here I call the Socket Method (<code>portIsOpen</code>) and if it returns true, I append the IP Adress that was sent to the Method to my <strong>Stringbuilder</strong> <code>possibleIPAddresses</code>. This way I can list all the IP-Addresses that have the Port open and then later connect to them.</p>
  
  <pre><code>//Initialisiert Variablen für den Port Prüfer
  public StringBuilder possibleIPAddress = new StringBuilder("");
  final ExecutorService es = Executors.newFixedThreadPool(50);
  final int timeout = 500;
  final List&lt;Future&lt;Boolean&gt;&gt; futures = new ArrayList&lt;&gt;();
  
  //Code for querying IP Adress here.
  
  //Checking all IP-Addresses from 10.0.0.1-10.0.0.255 for open SSH Port
  String ip = "";
  for (int x=1;x&lt;=10;x++) {
      ip = "10.0.0."+x;
  
      //Should return true if port on IP-Address is open, but returns true everytime..help?
      if (futures.add(portIsOpen(es, ip, 22, timeout))) {
          possibleIPAddress.append(ip).append("\n");
      }
  
      es.shutdown();
  
  }
  </code></pre>
  
  <p>And here is the Code for the port scanner (It is the same as in the port scanner question)</p>
  
  <pre><code>public static Future&lt;Boolean&gt; portIsOpen(final ExecutorService es, final String ip, final int port, final int timeout) {
      return es.submit(new Callable&lt;Boolean&gt;() {
          @Override public Boolean call() {
              try {
                  Socket socket = new Socket();
                  socket.connect(new InetSocketAddress(ip, port), timeout);
                  socket.close();
                  return true;
              } catch (Exception ex) {
                  return false;
              }
          }
      });
  }
  </code></pre>