如何检查端口是否在客户端的网络/防火墙处打开? [英] How to check whether a port is open at client's network/firewall?

查看:456
本文介绍了如何检查端口是否在客户端的网络/防火墙处打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


最后使用jQuery AJAX(和JSONP)的timeout属性解决了这个问题。看看我自己的答案!

This is solved at last with "timeout" attribute of jQuery AJAX (and JSONP). See my own answer !

请参阅更新的部分,我也尝试过applet。如果您能通过applet实现提供解决方案,我们会毫不犹豫地接受您的答案。

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

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 xhr.status == 200 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.

任何替代解决方案(必须是客户端的东西必须与我的应用程序并行,请不要建议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
}

---- -------------------------------------------------- --- UPDATE ---------------------------------------------- ------------------

我尝试使用Java Applet(感谢Y Martin的建议) 。这在appletviewer中工作正常。但是当我在HTML页面中添加applet时,它会提供易受攻击的结果。从某种意义上说,当我更改选项卡或调整浏览器大小时,在打印的消息中正在更改 portAvailable 的值。

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 portAvailable is being altered in the printed message.

小程序代码:

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页面(我正在托管它在同一台计算机上使用不同的Tomcat 6在端口9090上运行。我可以使用url查看此页面---> http://101.220.25.76:9090/test/ ):

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 ---> http://101.220.25.76:9090/test/):

<html>
<body>
        <applet code="ConnectionTestApplet" width=300 height=50>
        </applet>
</body>
</html>

我是如何进行端口1935阻止和打开的?

我为端口1935创建了入站和出站的防火墙规则。
我通过禁用/启用这两个规则检查端口1935打开/阻止方案。

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.

这是我的 SSCCE 。现在请帮助我:))

This is my S.S.C.C.E. Now please help me :)

推荐答案

陷入困境!我用JSONP和jQuery AJAX调用解决了我的问题。我发现了jQuery AJAX的 timeout 属性,当端口被阻塞或打开时,我的代码流畅地执行。这是未来访客的解决方案。感谢所有回复者。

Gotcha !!! I have solved my problem with JSONP and jQuery AJAX call. I discovered the timeout attribute of jQuery AJAX and my code executed fluently when the port was blocked or opened. Here is the solution for future visitors. Thanks to all answerers for contribution.

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

这篇关于如何检查端口是否在客户端的网络/防火墙处打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆