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

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

问题描述

这最终通过 jQuery AJAX(和 JSONP)的超时"属性解决了.看我自己的回答!

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

请看更新的部分,我也用小程序试过.如果您可以提供小程序实现的解决方案,将毫不犹豫地接受您的回答.

我正在使用基于 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
}

--------------------------------------------- - - - - - -更新 - - - - - - - - - - - - - - - - - -----------------------------

我已经尝试过 Java Applet(感谢 Y Martin 的建议).这在 appletviewer 中运行良好.但是当我在 HTML 页面中添加小程序时,它给出了易受攻击的结果.从某种意义上说,当我更改选项卡或调整浏览器大小时,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.

这是我的S.S.C.C.E.现在请帮助我:)

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天全站免登陆