add getNetListenAddress method
This commit is contained in:
parent
c97c11fe4b
commit
041ab0034f
|
@ -637,6 +637,46 @@ public class Sigar implements SigarProxy {
|
|||
public native NetConnection[] getNetConnectionList(int flags)
|
||||
throws SigarException;
|
||||
|
||||
/**
|
||||
* Get the TCP listen address for the given port.
|
||||
* If the port is not bound to a specific address,
|
||||
* the loopback address will be returned.
|
||||
* If there is not a listener on the given port, null will be returned.
|
||||
*/
|
||||
public String getNetListenAddress(long port)
|
||||
throws SigarException {
|
||||
|
||||
int flags = NetFlags.CONN_SERVER | NetFlags.CONN_TCP;
|
||||
|
||||
NetConnection[] connections =
|
||||
getNetConnectionList(flags);
|
||||
|
||||
for (int i=0; i<connections.length; i++) {
|
||||
NetConnection conn = connections[i];
|
||||
|
||||
if (conn.getState() != NetFlags.TCP_LISTEN) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (conn.getLocalPort() == port) {
|
||||
if (conn.getLocalAddress().equals(NetFlags.ANY_ADDR)) {
|
||||
return NetFlags.LOOPBACK_ADDRESS;
|
||||
}
|
||||
else {
|
||||
return conn.getLocalAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getNetListenAddress(String port)
|
||||
throws SigarException {
|
||||
|
||||
return getNetListenAddress(Long.parseLong(port));
|
||||
}
|
||||
|
||||
public NetStat getNetStat()
|
||||
throws SigarException {
|
||||
return new NetStat(this);
|
||||
|
|
|
@ -129,6 +129,12 @@ public interface SigarProxy {
|
|||
public NetConnection[] getNetConnectionList(int flags)
|
||||
throws SigarException;
|
||||
|
||||
public String getNetListenAddress(long port)
|
||||
throws SigarException;
|
||||
|
||||
public String getNetListenAddress(String port)
|
||||
throws SigarException;
|
||||
|
||||
public NetStat getNetStat() throws SigarException;
|
||||
|
||||
public Who[] getWhoList() throws SigarException;
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package net.hyperic.sigar.test;
|
||||
|
||||
import net.hyperic.sigar.NetConnection;
|
||||
import net.hyperic.sigar.NetFlags;
|
||||
import net.hyperic.sigar.NetStat;
|
||||
import net.hyperic.sigar.Sigar;
|
||||
import net.hyperic.sigar.SigarNotImplementedException;
|
||||
|
@ -23,5 +25,16 @@ public class TestNetStat extends SigarTestCase {
|
|||
assertGtEqZeroTrace("Outbound", netstat.getTcpOutboundTotal());
|
||||
assertGtEqZeroTrace("Inbound", netstat.getTcpInboundTotal());
|
||||
assertGtEqZeroTrace("Listen", netstat.getTcpListen());
|
||||
|
||||
int flags = NetFlags.CONN_SERVER | NetFlags.CONN_TCP;
|
||||
|
||||
NetConnection[] connections =
|
||||
sigar.getNetConnectionList(flags);
|
||||
|
||||
for (int i=0; i<connections.length; i++) {
|
||||
long port = connections[i].getLocalPort();
|
||||
traceln("Listen " +
|
||||
sigar.getNetListenAddress(port) + ":" + port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue