How to check port availability in client-side JavaScript?

Is it possible to detect in JavaScript (in the browser) if the port is disabled by a firewall or router?

+6
source share
3 answers

You can see if there is an expected answer or not.

When using javascript, you need to stay within HTTP bounds.

Of course, you can send an Ajax request to any server port and see if you have an error. If you want to check the port for the current computer, perhaps sending a request to "localhost: 843" may help.

But the error may be due to some other reasons, and not to the problem of the firewall.

We need more information to help you.

+4
source

No, with pure javascript this is not possible (except for creating HTTP requests for specific ports, but these results are insignificant), however you can check from the outside (in other words, your server) regardless of whether the port is open. Another option would be to use a java applet or browser plugin that could do this for you if you really need it, in which case there are various open source tools that you could probably port if you have the necessary experience with them. However, note that this is not entirely convenient. (In any case, it would be helpful if you could describe the exact scenario where you need it, as there might be a completely different solution.)

+4
source

If you are flexible enough to use jQuery, see this Answer from me . This will not only check for the presence of the port, but also whether the success response code 200 will come from a remote (or any, I meant that it also supports cross-domain) server. We also give a solution here. I will check port 843 here.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <script type="text/javascript" src="jquery-1.7.2-min.js"></script> </head> <body> <script type"text/javascript"> var isAccessible = null; function checkConnection() { /*make sure you host a helloWorld HTML page in the following URL, so that requests are succeeded with 200 status code*/ var url = "http://yourserverIP:843/test/hello.html" ; $.ajax({ url: url, type: "get", cache: false, dataType: 'jsonp', // it is for supporting crossdomain crossDomain : true, asynchronous : false, jsonpCallback: 'deadCode', timeout : 1500, // set a timeout in milliseconds complete : function(xhr, responseText, thrownError) { if(xhr.status == "200") { isAccessible = true; success(); // yes response came, execute success() } else { isAccessible = false; failure(); // this will be executed after the request gets timed out due to blockage of ports/connections/IPs } } }); } $(document).ready( function() { checkConnection(); // here I invoke the checking function }); </script> </body> </html> 
+3
source

Source: https://habr.com/ru/post/911725/


All Articles