Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查客户端的端口是否打开';网络/防火墙?_Java_Javascript_Jquery_Jakarta Ee_Jsonp - Fatal编程技术网

Java 如何检查客户端的端口是否打开';网络/防火墙?

Java 如何检查客户端的端口是否打开';网络/防火墙?,java,javascript,jquery,jakarta-ee,jsonp,Java,Javascript,Jquery,Jakarta Ee,Jsonp,最后通过jQueryAjax(和JSONP)的“timeout”属性解决了这个问题。看看我自己的答案 请查看更新的部分,我也尝试过使用applet。如果您能通过小程序实现给出解决方案,我们将毫不犹豫地接受您的答案。 我正在使用一个基于Java的web应用程序。我的要求是检查特定端口(比如1935)在客户端是否打开或阻塞。我已经实现了一个“jsonp”(为什么是“jsonp”?我发现通过AJAX的“http”请求不能用于浏览器的corssdomain的同源策略)AJAX调用,它调用了我的一个包含特

最后通过jQueryAjax(和JSONP)的“timeout”属性解决了这个问题。看看我自己的答案

请查看更新的部分,我也尝试过使用applet。如果您能通过小程序实现给出解决方案,我们将毫不犹豫地接受您的答案。

我正在使用一个基于Java的web应用程序。我的要求是检查特定端口(比如1935)在客户端是否打开或阻塞。我已经实现了一个“jsonp”(为什么是“jsonp”?我发现通过AJAX的“http”请求不能用于浏览器的corssdomain的同源策略)AJAX调用,它调用了我的一个包含特定端口的服务器。如果服务器返回
xhr.status==200
则端口打开。这里有一个缺点,我不能让执行流等待(同步)直到调用完成。下面是我正在使用的JavaScript函数

也欢迎任何替代解决方案(必须是客户端解决方案,必须与我的应用程序并行,请不要建议使用python/php/其他语言)。谢谢您的时间

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==> " + isAccessible); // this alert does not come when port is blocked
            }
        }
    });

    alert("returning isAccessible=> "+ 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
}
-------------------------------------------------------------更新--------------------------------------------------------------------

我试过使用Javaapplet(多亏了Y Martin的建议)。这在appletviewer中运行良好。但当我在HTML页面中添加小程序时,它会给出易受攻击的结果。从某种意义上说,当我更改选项卡或调整浏览器大小时,
portAvailable
的值在打印消息中被更改

小程序代码:

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--> 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--->  " + portAvailable);
        }
        catch (Exception e) {
            portAvailable = false;
            System.out.println("init() giving--->  " + portAvailable);
            System.out.println("Threw error---> " + e.getMessage());
        }

    }
    public void paint(Graphics g) {
        System.out.println("Connection possible---> " + portAvailable);
        String msg = "Connection possible---> " + portAvailable;
        g.drawString(msg, 10, 30);
    }
}
这是我的HTML页面(我用运行在端口9090上的不同Tomcat 6在同一台计算机上托管它。我可以使用url查看此页面-->
):



. 现在请帮助我:)

我认为您不了解JSONP的用例,并且不可能用它测试开放端口


如果您想要客户端解决方案,可以使用WebSocket,但这仅适用于chrome或ff等新浏览器。否则,请求执行ping的服务器端脚本。例如,使用curl脚本:

您不能在JavaScript中执行此操作,因为它没有真正的套接字支持,而使用JavaScript只能测试HTTP套接字的存在。您可以使用Java(JavaScript不是Java)并编写一个适当的Java小程序来实现它

你也应该读一下这个


尝试在JavaScript中使用

,您必须解决异步问题。以下是一项建议:

  • HTML页面将动画图像显示为进度条
  • 您可以调用
    检查URL
  • 在收到回调或定义的超时后,您可以更改错误消息的显示,或者继续执行要执行的作业
基于使用
XMLHttpRequest
,下面是
checkURL
的代码示例:

var myrequest = new ajaxRequest();
var isAccessible = false;
myrequest._timeout = setTimeout(function() {
      myrequest.abort();
      displayErrorMessage();
    },
    1000
    ) //end setTimeout
myrequest.onreadystatechange = function() {
    if (myrequest.readyState == 4) { //if request has completed
      if (myrequest.status == 200) {
        isAccessible = false;
        goOnWithTheJob();
      } else {
        displayErrorMessage();
      }
    }
myrequest.open("GET", url, true);
myrequest.send(null); //send GET request
// do nothing - wait for either timeout or readystate callback 
这段代码允许1秒从基本资源上的HTTP get获取200响应

在您的本地测试中,您会立即得到答案,因为如果端口关闭,但防火墙不响应,系统会响应连接重置


即使可以同步使用
open
方法,我也建议使用计时器,因为代码可能等待TCP超时和重试(3 x 1分钟?),因为防火墙通常只会在关闭的端口上丢弃数据包,并可能拒绝ICMP数据包,从而通过ping阻止您测试可用性。我想这样的检查不需要这么长的等待时间。

可以使用flash组件代替小程序。使用ActionCCcript中提供的套接字类,可以打开从闪存到服务器端口的tcp连接,以检查其是否打开。但是,根据flash player版本,需要在打开套接字的服务器上放置一个策略文件。

以下是一个Java代码,用于测试服务器/端口连接:

import java.io.*;
import java.net.*;

public class ConnectionTestApplet extends Applet {
    public void start() {
        boolean portAvailable = false;
        int delay = 1000; // 1 s
        try {
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress("server.domain.com", 1935), delay);
            portAvailable = socket.isConnected();
            socket.close();
        } catch (UnknownHostException uhe) {
            uhe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        System.out.println("Connection possible: " + portAvailable);
    }
}
您仍然必须从小程序中获取信息,才能对该结果执行其他操作。最简单的方法是通过
getAppletContext(),重定向浏览器。showDocument(url)

查看以下内容:

使用JS Recon,您可以使用javascript进行端口扫描。您可以简单地将它指向您的本地IP地址。我相信它是通过将web套接字/cors连接到任意设计的ip/套接字并测量超时来工作的。这不是一个完美的方法,但这最终可能是javascript的局限性


如果您可以在java小程序/flash应用程序中实现,那么最终可能会更好,因为它们具有较低级别的访问权限。

明白了!!!我已经用JSONP和jQueryAjax调用解决了我的问题。我发现jqueryajax的
timeout
属性,当端口被阻塞或打开时,我的代码可以流畅地执行。这是未来访客的解决方案。感谢所有回答者的贡献

<!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() {
                var url = "http://101.212.33.60:1935/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, esecute 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>

var isAccessible=null;
函数checkConnection(){
变量url=”http://101.212.33.60:1935/test/hello.html" ;
$.ajax({
url:url,
键入:“获取”,
cache:false,
数据类型:'jsonp',//用于支持跨域
跨域:是的,
异步:false,
jsonpCallback:“死代码”,
超时:1500,//以毫秒为单位设置超时
完成:函数(xhr、responseText、thrownError){
如果(xhr.status==“200”){
isAccessible=true;
success();//回答是,esecute success()
}
否则{
isAccessible=假;
failure();//这将在请求之后执行
<!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() {
                var url = "http://101.212.33.60:1935/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, esecute 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>