java - Java中如何查找IP地址建立的TCP连接数?

标签 java networking tcp ip-address spoofing

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

最佳答案

执行 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) {}
    }
}

希望对您有所帮助。

关于java - Java中如何查找IP地址建立的TCP连接数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36478395/

相关文章:

python - 如何在不绑定(bind) IP 的情况下在特定端口上运行服务

java - BigDecimal 与 BigInteger 和 BigDecimal 的性能比较

java - 使用 StAX 为 XML 创建索引以便快速访问

java - 使用 joda 格式化某些时间跨度时的奇怪现象

wcf - TCP 连接和防火墙

c++ - Qt 中的非阻塞线程 tcpi 客户端

java - Maven2 和 Swing 项目 : Build & run swing application

c++ - 具有多个网络接口(interface)的计算机上的 gethostname

java - 是什么导致此错误 (java) : "Invalid memory access of location 0x0 rip=0x106282bae"

swift - RxSwift 链接请求