List all network connection with Java

I’d like to write a Java program that will list all open network connection on a machine, in a host:port pattern.

Incoming connections are more important, but outgoing connections are nice to have as well.

Currently I have 2 options in mind:

  1. Use jpcap to monitor all packets and get the host:port of incomingoutgoing network connections out of it. I’m afraid that this option is a bit of an overkill and will have performance implication.
  2. Use netstat (or similar commands per OS type) and parse it. Per this option, I also thought of using SIGAR, but as for now I was only able to get some netstat metrics rather than a list of network connections.
    This options seems pretty easy to implement and straight forward, but I’m afraid I’m missing something here.

Do you guys have a different idea, or any recommendation or comments as to the two options?

Thanks


Source: java

Leave a Reply