user2572969
user2572969

Reputation: 267

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.

Upvotes: 1

Views: 1552

Answers (2)

Marcin Łoś
Marcin Łoś

Reputation: 3246

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.

Upvotes: 0

faisalbhagat
faisalbhagat

Reputation: 2150

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(); 
            } 

        }

Upvotes: 1

Related Questions