map listening tcp ports to connected remote addresses

This commit is contained in:
Doug MacEachern 2005-03-11 01:48:56 +00:00
parent 058a459872
commit 3d0dc203d8
1 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,71 @@
package net.hyperic.sigar;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class NetPortMap {
/**
* Map listening tcp ports to connected remote addresses.
* key == Listening tcp port on the local machine.
* value == List of connected remote addresses.
*/
public static Map getTcpConnections(Sigar sigar)
throws SigarException {
int flags =
NetFlags.CONN_SERVER | NetFlags.CONN_TCP;
Map map = new HashMap();
NetConnection[] connections =
sigar.getNetConnectionList(flags);
//first pass, get listening port numbers
for (int i=0; i<connections.length; i++) {
NetConnection conn = connections[i];
Long port = new Long(conn.getLocalPort());
List addresses = (List)map.get(port);
if (addresses == null) {
addresses = new ArrayList();
map.put(port, addresses);
}
}
//second pass, get addresses connected to listening ports
flags = NetFlags.CONN_CLIENT | NetFlags.CONN_TCP;
connections = sigar.getNetConnectionList(flags);
for (int i=0; i<connections.length; i++) {
NetConnection conn = connections[i];
Long port = new Long(conn.getLocalPort());
List addresses = (List)map.get(port);
if (addresses == null) {
continue;
}
addresses.add(conn.getRemoteAddress());
}
return map;
}
public static void main(String[] args) throws Exception {
Sigar sigar = new Sigar();
Map ports = getTcpConnections(sigar);
for (Iterator it = ports.entrySet().iterator();
it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
Long port = (Long)entry.getKey();
List addresses = (List)entry.getValue();
System.out.println(port + "=" + addresses);
}
}
}