title: Socket connection succeeds even with wrong/null URL
body: <p>I am running a test using the Java socket class, but somehow my socket.connect ALWAYS connects successfully to something, even if my url variable is null or incorrect. Does anyone know why?</p>
  <pre><code>package ping_run_send;
  
  import java.util.*;
  import java.lang.*;
  import java.net.*;
  import java.io.*
  import java.security.cert.*;
  import javax.net.ssl.*;
  
  public class tcpping {
      private String url, status;
      private Date timestamp;
      private long rtime;
      
      tcpping(String input_url){
          this.url = input_url;
      }
      void ping() {
          try{
              Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(&quot;my proxy&quot;, 80));
              Socket socket = new Socket(proxy);
              long tStart = System.currentTimeMillis();
              socket.connect(new InetSocketAddress(url, 80),2000);
              long tEnd = System.currentTimeMillis();
              rtime =  tEnd-tStart;
              timestamp = new Date(tStart);
              
              InputStream sIn = socket.getInputStream();
              
              if (sIn != null) {
                  status = &quot;normal&quot;;
                  socket.close();
              }else {
                  status = &quot;error&quot;;
              }
          } catch(final MalformedURLException e){
              status = &quot;error&quot;;
          } catch(IOException e){
              status = &quot;error&quot;;
          }
      }
      Long get_rtime() {
          return rtime;
      }
      Date get_timestamp() {
          return timestamp;
      }
      String get_status() {
          return status;
      }
      
  }
  
  </code></pre>
  <p>I also tried changing the if statement from isConnected() to</p>
  <pre><code>            InputStream sIn = socket.getInputStream();
              if (sIn != null) {
                  status = &quot;normal&quot;;
                  socket.close();
              }else {
                  status = &quot;error&quot;;
              }
  </code></pre>
  <p>But nothing seems to be have changed on its ability to detect connection error.</p>
  <p>my testing file:</p>
  <pre><code>package ping_run_send;
  import java.lang.*;
  import java.util.*;
  import java.io.*;
  
  import ping_run_send.httpping;
  
  public class main {
  
      public static void main(String[] args){ 
          String url = &quot;http://google.com&quot;;
          //urls like &quot;&quot;,http://gowegwgwoJOIJOI03OINEPJPogle.#(*%&quot; all works somehow
          
          tcpping testTcp = new tcpping(url);
          testTcp.ping();
          System.out.println(&quot;tcp ping:&quot;);
          System.out.println(testTcp.get_rtime());
          System.out.println(testTcp.get_timestamp());
          System.out.println(testTcp.get_status());
          
  
          
      }
  }
  
  </code></pre>