简体   繁体   中英

how to Monitor tcp stats in windows with java program

I want to monitor how many TCP connections that my system has through JAVA program. In Linux, there is file that contains all the info about TCP connections. Name of the file is /proc/net/tcp From that file I can monitor but is there any file like that in windows so that I can monitor TCP data from that file.

you can use the netstat command. You can execute this command from java by using RunTime class

        { 
            Process p=Runtime.getRuntime().exec("cmd /c netstat"); 
            p.waitFor(); 
            BufferedReader reader=new BufferedReader(
                new InputStreamReader(p.getInputStream())
            ); 
            String line=reader.readLine(); 
            while(line!=null) 
            { 
                System.out.println(line); 
                line=reader.readLine(); 
            } 

        }

It seems JMX is just what you need. You did not specify what precise information you need, but there are lots of ready-to-use statistics provided by the JVM, including file descriptor usage. You might want to explore it with JConsole first - here is an introduction to JConsole from Oracle.

Edit: while there does not seem to be any standard way to obtain precisely TCP connections count, there is a linux-specific way to get total number of open file descriptors: OperatingSystem.OpenFileDescriptorCount property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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