title: How to check whether a port is open at client's network/firewall?
body: <blockquote>
    <p>This is solved at last with "timeout" attribute of jQuery AJAX (and JSONP). See my own answer !</p>
  </blockquote>
  
  <p><strong>Please see the updated part, I have tried with applet too. And will not hesitate to accept your answer if you can give a solution with applet implementation.</strong></p>
  
  <p>I am working with a Java based web application. My requirement is to check whether a particular port (say 1935) is open or blocked at client's end. I have implemented a "jsonp" (why 'jsonp' ? i found that 'http' request through AJAX cannot work for corssdomain for browsers 'same origin policy') AJAX call to one of my server containing particular port. And if the server returns <code>xhr.status == 200</code> the port is open. Here is a drawback that I can't make the execution-flow wait (synchronous) until the call completes. Here is the JavaScript function I am using.</p>
  
  <p><strong>Any alternative solution (must be a client-sided thing must be parallel with my application, please dont suggest python/php/other languages) is also welcome.</strong> Thanks for your time.</p>
  
  <pre><code>function checkURL() {
  
      var url = "http://10.0.5.255:1935/contextname" ;
      var isAccessible = false;
  
      $.ajax({
          url: url,
          type: "get",
          cache: false,
          dataType: 'jsonp',
          crossDomain : true,
          asynchronous : false,
          jsonpCallback: 'deadCode',
          complete : function(xhr, responseText, thrownError) {
              if(xhr.status == "200") {
                  isAccessible = true;
                  alert("Request complete, isAccessible==&gt; " + isAccessible); // this alert does not come when port is blocked
              }
          }
      });
  
      alert("returning isAccessible=&gt; "+ isAccessible); //this alert comes 2 times before and after the AJAX call when port is open
      return isAccessible;
  
  }
  
  function deadCode() {
      alert("Inside Deadcode"); // this does not execute in any cases
  }
  </code></pre>
  
  <p><strong>---------------------------------------------------------UPDATE----------------------------------------------------------------</strong></p>
  
  <p>I have tried with Java Applet (thanks to Y Martin's suggestion). This is working fine in appletviewer. But when I add the applet in HTML page, it is giving vulnerable results. Vulnerable in the sense, when I change the tab or resize the browser, the value of <code>portAvailable</code> is being altered in the printed message. </p>
  
  <p>Applet Code :</p>
  
  <pre><code>import java.applet.Applet;
  import java.awt.Graphics;
  import java.net.InetSocketAddress;
  import java.net.Socket;
  
  public class ConnectionTestApplet extends Applet {
      private static boolean portAvailable;
      public void start() {
          int delay = 1000; // 1 s
          try {
              Socket socket = new Socket();
                  /*****This is my tomcat5.5 which running on port 1935*************/
                  /***I can view it with url--&gt; http://101.220.25.76:1935/**********/
              socket.connect(new InetSocketAddress("101.220.25.76", 1935), delay);
              portAvailable = socket.isConnected();
              socket.close();
              System.out.println("init() giving---&gt;  " + portAvailable);
          }
          catch (Exception e) {
              portAvailable = false;
              System.out.println("init() giving---&gt;  " + portAvailable);
              System.out.println("Threw error---&gt; " + e.getMessage());
          }
  
      }
      public void paint(Graphics g) {
          System.out.println("Connection possible---&gt; " + portAvailable);
          String msg = "Connection possible---&gt; " + portAvailable;
          g.drawString(msg, 10, 30);
      }
  }
  </code></pre>
  
  <p>And this is my HTML page (I am hosting it on same computer with a different Tomcat 6 which runs on port 9090. I can view this page with url ---> <code><a href="http://101.220.25.76:9090/test/" rel="nofollow">http://101.220.25.76:9090/test/</a></code>):</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
          &lt;applet code="ConnectionTestApplet" width=300 height=50&gt;
          &lt;/applet&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>And how I am doing the port 1935 blocking and openning ?</strong></p>
  
  <p>I have created firewall rule for both inbound and outbound for port 1935.
  I check the port 1935 open/blocked scenario by disabling/enabling both rules.</p>
  
  <p>This is my <a href="http://sscce.org/" rel="nofollow">S.S.C.C.E</a>. Now please help me :)</p>