Logger.getLogger(NetStat.class.getName()).log(Level.SEVERE, null, ex);
}
for (int i = 0; i < connections.length; i++) {
NetConnection conn = connections[i];
NetStatObject netObj = new NetStatObject();
String proto = conn.getTypeString();
String state;
if (conn.getType() == NetFlags.CONN_UDP) {
state = "";
} else {
state = conn.getStateString();
}
netObj.setType(proto);
netObj.setLocalAddress(formatAddress(conn.getType(),
conn.getLocalAddress(),
conn.getLocalPort(),
LADDR_LEN));
netObj.setRemoteAddress(formatAddress(conn.getType(),
conn.getRemoteAddress(),
conn.getRemotePort(),
RADDR_LEN));
netObj.setState(state);
String process = null;
if (wantPid &&
//XXX only works w/ listen ports
(conn.getState() == NetFlags.TCP_LISTEN)) {
try {
long pid =
sigar.getProcPort(conn.getType(),
conn.getLocalPort());
if (pid != 0) { //XXX another bug
String name =
sigar.getProcState(pid).getName();
process = pid + "/" + name;
}
} catch (SigarException e) {
}
}
if (process == null) {
process = "";
}
netObj.setProcess(process);
netVector.add(netObj);
}
return netVector;
}