如何在客户端 JavaScript 中检查端口可用性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9884920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check port availability in client-side JavaScript?
提问by Danny Fox
Is it possible to detect in JavaScript (in the browser) if a port is disabled by the firewall or router?
是否可以在 JavaScript(在浏览器中)检测端口是否被防火墙或路由器禁用?
回答by David Mulder
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. Another option would be to use a java applet or browser plugin which could do this for you if you reallyneed it, in which case there are various open source tools which you could probably port if you have the necessary experience with those. 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.)
不,使用纯 javascript 这是不可能的(除了向特定端口发出 http 请求,但这些结果意义不大),但是您可以做的是从外部(即您的服务器)检查特定端口是否打开. 另一种选择是使用 Java 小程序或浏览器插件,如果您确实需要它,它们可以为您执行此操作,在这种情况下,如果您有必要的经验,可以移植各种开源工具。但是请注意,这并不完全是用户友好的。(无论哪种方式,如果您能描述您需要它的确切场景,那将会很有用,因为可能有一个完全不同的解决方案。)
回答by Sandeep
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. If you want to check port for current machine then probably sending a request on "localhost:843" could help.
当然,您可以在服务器的任何端口上发送 Ajax 请求,然后查看是否出现错误。如果您想检查当前机器的端口,那么可能在“localhost:843”上发送请求可能会有所帮助。
But the error could be of some other reasons and not necessarily firewall issue.
但错误可能是其他一些原因,不一定是防火墙问题。
We need more information to help you out.
我们需要更多信息来帮助您。
回答by tusar
If you are flexible enough to use jQuery, then see this Answer by me. 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. Also giving the solution here. I will be checking here for port 843.
如果您足够灵活地使用 jQuery,请参阅我的这个答案。这不仅会检查端口的可用性,还会检查成功响应代码 200 是否来自远程(或任何,我的意思是它也支持跨域)服务器。也在这里给出解决方案。我将在这里检查端口 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>