Interface InternetProtocolStats
- All Known Implementing Classes:
AbstractInternetProtocolStats, LinuxInternetProtocolStats
Provides key statistics for TCP and UDP network protocols, including aggregate counters and per-connection details.
Use getTCPv4Stats() and getTCPv6Stats() for aggregate TCP counters (connections established,
active, passive, failures, resets, and segment counts). Use getUDPv4Stats() and getUDPv6Stats() for
aggregate UDP counters (datagrams sent, received, and errors).
Use getConnections() to list individual active TCP and UDP connections, each represented as an
InternetProtocolStats.IPConnection with local/remote addresses, ports, state, and owning process ID.
Example: listing active TCP connections:
InternetProtocolStats ipStats = os.getInternetProtocolStats();
for (IPConnection conn : ipStats.getConnections()) {
if (conn.getType().startsWith("tcp")) {
String local = addrToString(conn.getLocalAddress());
String foreign = addrToString(conn.getForeignAddress());
System.out.printf("%s:%d -> %s:%d (%s)%n", local, conn.getLocalPort(), foreign, conn.getForeignPort(),
conn.getState());
}
}
// Helper to safely convert address bytes to a string
static String addrToString(byte[] addr) {
try {
return addr.length > 0 ? InetAddress.getByAddress(addr).getHostAddress() : "*";
} catch (UnknownHostException e) {
return "?";
}
}
Platform notes: On macOS, connection information requires elevated permissions. Without elevated permissions,
TCP segment data is estimated.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classEncapsulates information associated with an IP connection.static enumThe TCP connection state as described in RFC 793.static final classEncapsulates statistics associated with a TCP connection.static final classEncapsulates statistics associated with a UDP connection. -
Method Summary
Modifier and TypeMethodDescriptionGets a list of TCP and UDP connections.Get the TCP stats for IPv4 connections.Get the TCP stats for IPv6 connections, if available.Get the UDP stats for IPv4 datagrams.Get the UDP stats for IPv6 datagrams, if available.
-
Method Details
-
getTCPv4Stats
InternetProtocolStats.TcpStats getTCPv4Stats()Get the TCP stats for IPv4 connections.On macOS connection information requires elevated permissions. Without elevated permissions, segment data is estimated.
- Returns:
- a
InternetProtocolStats.TcpStatsobject encapsulating the stats.
-
getTCPv6Stats
InternetProtocolStats.TcpStats getTCPv6Stats()Get the TCP stats for IPv6 connections, if available. If not available separately, these may be 0 and included in IPv4 connections.- Returns:
- a
InternetProtocolStats.TcpStatsobject encapsulating the stats.
-
getUDPv4Stats
InternetProtocolStats.UdpStats getUDPv4Stats()Get the UDP stats for IPv4 datagrams.- Returns:
- a
InternetProtocolStats.UdpStatsobject encapsulating the stats.
-
getUDPv6Stats
InternetProtocolStats.UdpStats getUDPv6Stats()Get the UDP stats for IPv6 datagrams, if available. If not available separately, these may be 0 and included in IPv4 datagrams.- Returns:
- a
InternetProtocolStats.UdpStatsobject encapsulating the stats.
-
getConnections
List<InternetProtocolStats.IPConnection> getConnections()Gets a list of TCP and UDP connections.- Returns:
- A list of
InternetProtocolStats.IPConnectionobjects for TCP and UDP connections.
-