【问题标题】:How to find the number of TCP connections established by an IP address in Java?如何在Java中查找IP地址建立的TCP连接数?
【发布时间】:2016-07-28 11:10:22
【问题描述】:

我正在尝试实现跳数过滤算法。为了更新 HCF 表以防止 IP 欺骗,我需要在 Java 中 IP 地址的 TCP 连接处于已建立状态时更新计数器。

【问题讨论】:

  • 是否必须连接到您的应用程序或运行应用程序的机器?
  • 运行它的机器。这是一个入侵检测系统。

标签: java networking tcp ip-address spoofing


【解决方案1】:

执行netstat -at | grep ESTA,您将获得在您的机器中建立的所有 TCP 连接。要在 Java 中运行此命令,您可以使用 Runtime.getRuntime().exec(),如下所示

public class Test {
    public static void main(String args[]) {
        String s;
        Process p;
        try {
            p = Runtime.getRuntime().exec("netstat -at | grep ESTA");
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

            while ((s = br.readLine()) != null){

                // Update here counters if an IP have established a new TCP connection
                System.out.println("line: " + s);
            }

            p.waitFor();
            p.destroy();
        } catch (Exception e) {}
    }
}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2012-10-01
    • 1970-01-01
    • 2015-06-16
    相关资源
    最近更新 更多