VMware Cloud Community
manojpec
Contributor
Contributor

NetStat getTcpEstablished gives wrong number

Hi,

Have a look at the following code snippet and the sample output. I am not getting the expected result on invoking netstat.getTcpEstablished();


netstat = sigar.getNetStat(serverLsnr.getBindAddress().getAddress(), serverLsnr.getBindPort());
getNetInfo(serverLsnr.getBindPort());
Assert.assertEquals(2, netstat.getTcpEstablished());


private void getNetInfo(int bindPort) {
try {
Sigar s = new Sigar();
NetInfo info = s.getNetInfo();
NetInterfaceConfig config = s.getNetInterfaceConfig(null);
System.out.println(info.toString());
System.out.println(config.toString());

int flags = NetFlags.CONN_TCP | NetFlags.TCP_ESTABLISHED;

NetConnection[] connections = s.getNetConnectionList(flags);

System.out.println("XXX Established connections if any");
for (int i = 0; i<connections.length; i++) {
long port = connections(i).getLocalPort();
long remotePort = connections(i).getRemotePort();
if (bindPort == port || bindPort == remotePort) {
System.out.println("XXX " + connections(i));
}
}
} catch (SigarException se) {
System.out.println("Unable to get Sigar NetInfo: " + se);
}
}


output:

INFO XXX Established connections if any
INFO XXX {Type=16, RemoteAddress=::ffff:127.0.0.1, State=6, SendQueue=0, RemotePort=51091, LocalAddress=::ffff:127.0.0.1, ReceiveQueue=0, LocalPort=39852}
INFO XXX {Type=16, RemoteAddress=::ffff:127.0.0.1, State=1, SendQueue=0, RemotePort=51091, LocalAddress=::ffff:127.0.0.1, ReceiveQueue=0, LocalPort=34122}
INFO XXX {Type=16, RemoteAddress=::ffff:127.0.0.1, State=6, SendQueue=0, RemotePort=51091, LocalAddress=::ffff:127.0.0.1, ReceiveQueue=0, LocalPort=35317}
INFO XXX {Type=16, RemoteAddress=::ffff:127.0.0.1, State=6, SendQueue=0, RemotePort=51091, LocalAddress=::ffff:127.0.0.1, ReceiveQueue=0, LocalPort=54564}
INFO XXX {Type=16, RemoteAddress=::ffff:127.0.0.1, State=6, SendQueue=0, RemotePort=51091, LocalAddress=::ffff:127.0.0.1, ReceiveQueue=0, LocalPort=54566}
INFO XXX {Type=16, RemoteAddress=::ffff:127.0.0.1, State=6, SendQueue=0, RemotePort=51091, LocalAddress=::ffff:127.0.0.1, ReceiveQueue=0, LocalPort=54568}
INFO XXX {Type=16, RemoteAddress=::ffff:127.0.0.1, State=1, SendQueue=0, RemotePort=51091, LocalAddress=::ffff:127.0.0.1, ReceiveQueue=0, LocalPort=54570}
INFO XXX {Type=16, RemoteAddress=::ffff:127.0.0.1, State=6, SendQueue=0, RemotePort=51091, LocalAddress=::ffff:127.0.0.1, ReceiveQueue=0, LocalPort=54384}
INFO XXX {Type=16, RemoteAddress=::ffff:127.0.0.1, State=6, SendQueue=0, RemotePort=51091, LocalAddress=::ffff:127.0.0.1, ReceiveQueue=0, LocalPort=49485}
INFO XXX {Type=16, RemoteAddress=::ffff:127.0.0.1, State=1, SendQueue=0, RemotePort=34122, LocalAddress=::ffff:127.0.0.1, ReceiveQueue=0, LocalPort=51091}
INFO XXX {Type=16, RemoteAddress=::ffff:10.0.4.137, State=1, SendQueue=0, RemotePort=51091, LocalAddress=::ffff:10.0.4.160, ReceiveQueue=0, LocalPort=8080}
INFO XXX {Type=16, RemoteAddress=::ffff:127.0.0.1, State=1, SendQueue=0, RemotePort=54570, LocalAddress=::ffff:127.0.0.1, ReceiveQueue=0, LocalPort=51091}

NetStat.getTcpEstablished = 5.

1. Why is TIME_WAIT state is also counted for tcp established connections ?
2. What is the right way of getting current tcp established connections for particular address and pot ?
3. What is the right of listing all Tcp Established connections for a particular address.
Reply
0 Kudos
1 Reply
dougm_hyperic
VMware Employee
VMware Employee

Hi,

You should be able to use getNetStat(address, port) - what OS are running on? Any comparison to the netstat command?

For NetConnectionList flags, only NetFlags.CONN_* are valid bits, the TCP_ constants do not apply to the filter flags.
Reply
0 Kudos