简体   繁体   English

如何在客户端JavaScript中检查端口可用性?

[英]How to check port availability in client-side JavaScript?

是否可以在JavaScript中(在浏览器中)检测防火墙或路由器是否禁用了端口?

No, with pure javascript this is not possible (aside of making http requests to the specific ports, but those results mean little), what you can do however is check from the outside (in other words your server) whether the specific port is open. 不,使用纯JavaScript这是不可能的(除了向特定端口发出http请求,但这些结果意味着很少),但你可以做的是从外部检查(换句话说你的服务器)是否特定端口是打开的。 Another option would be to use a java applet or browser plugin which could do this for you if you really need it, in which case there are various open source tools which you could probably port if you have the necessary experience with those. 另一个选择是使用java applet或浏览器插件,如果你真的需要它可以为你做这个,在这种情况下有各种开源工具,如果你有必要的经验,你可以移植它们。 Do note however that this isn't exactly user friendly. 请注意,这并不完全是用户友好的。 (Either way, it would be useful if you would describe the exact scenario where you need this, as there might be an altogether different solution.) (无论哪种方式,如果您将描述您需要它的确切场景将会很有用,因为可能存在完全不同的解决方案。)

You can only see if the expected response is there or not. 您只能看到预期的响应是否存在。

One has to stay in the boundaries of HTTP when using javascript. 使用javascript时,必须留在HTTP的边界。

Of course you can send an Ajax request on whatever port of server and see if you get an error. 当然,您可以在任何服务器端口发送Ajax请求,看看是否收到错误。 If you want to check port for current machine then probably sending a request on "localhost:843" could help. 如果你想检查当前机器的端口,那么可能在“localhost:843”上发送请求可能有所帮助。

But the error could be of some other reasons and not necessarily firewall issue. 但错误可能是其他一些原因,而不一定是防火墙问题。

We need more information to help you out. 我们需要更多信息来帮助您。

If you are flexible enough to use jQuery, then see this Answer by me . 如果你足够灵活使用jQuery,那么请看我的回答 This will not only check the availability of port, but also whether a success response code 200 is coming from the remote (or any , I meant it supports cross-domain also) server. 这不仅会检查端口的可用性,还会检查成功响应代码200是来自远程(或任何,我的意思是它也支持跨域)服务器。 Also giving the solution here. 同样在这里给出解决方案。 I will be checking here for port 843. 我将在这里查看843号港口。

<!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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM