简体   繁体   English

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

[英]How to find the number of TCP connections established by an IP address in Java?

I'm trying to implement Hop Count Filtering Algorithm. 我正在尝试实现跳数过滤算法。 In order to update HCF table to prevent IP Spoofing, I need to update a counter when an IP address has TCP connections in established state in Java. 为了更新HCF表以防止IP欺骗,我需要在IP地址在Java中建立状态的TCP连接时更新计数器。

Excecuting the netstat -at | grep ESTA 超过netstat -at | grep ESTA netstat -at | grep ESTA you will get all TCP connections established in you machine. netstat -at | grep ESTA您将获得在您的机器中建立的所有TCP连接。 To run this command in Java you can use Runtime.getRuntime().exec() as follow 要在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) {}
    }
}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM