Eduardo Bezerra
Eduardo Bezerra

Reputation: 1953

How to measure system network usage programatically in Java?

I'd like to obtain the host's full network usage, that is, the current upload and download rates. How can I do that in Java? If it makes that easier, assume the platform is Linux.

Upvotes: 0

Views: 2567

Answers (2)

Stephen C
Stephen C

Reputation: 718688

How can I do that in Java?

Doing this in Java is a bad idea ... unless there is a strong requirement to have the information in a larger Java application.

  • You can't do it in pure Java. There are no standard Java APIs for accessing this kind of information.

  • I Googled for something Java-specific and free that matched your requirements, but didn't spot anything. (YMMV ... please feel free to repeat the search for yourself!).

  • There are OS specific tools for looking at network stats, etcetera, and one could attempt to run these tools as external processes from Java and "scrape" the output. But that would necessarily be OS / tool specific.

  • You could attempt to replicate what these tools are doing in Java, but this may involve native library calls, and would certainly be OS specific. (On Linux, you may be able to get the information you need by reading from the /proc file system ... but there could also be access control issues.)

Upvotes: 2

VictorCreator
VictorCreator

Reputation: 744

Nethogs: http://nethogs.sourceforge.net/ is probably the tool you're looking for.

Most people use prefer this as it splits bandwith usage up by process.

Upvotes: 0

Related Questions