Skip to main content

listening_sockets

Function listening_sockets 

pub fn listening_sockets(pid: u32) -> Result<Vec<ProcessSocket>, Error>
Available on crate feature net only.
Expand description

Query TCP listeners and bound UDP endpoints owned by pid.

The result is sorted and deduplicated (multiple descriptors can refer to the same endpoint). TCP connections and unbound UDP sockets are excluded. This does not follow child processes or identify the application role of a socket. Inherited sockets are included; during process startup, these can appear briefly before the child closes descriptors marked close-on-exec.

Supported on Linux, macOS and Windows. Other platforms return io::ErrorKind::Unsupported. Access restrictions are returned as errors, not an empty result. Sockets can disappear during enumeration, and a process can exit immediately after the query; a snapshot is not a reservation.

Linux joins the process’s socket descriptors to its network namespace’s /proc tables. Sockets moved between network namespaces may not be visible. Those tables do not expose IPv6 scope IDs, so Linux reports scope zero. macOS uses libproc and Windows uses the owner-PID IP Helper tables. These calls may block; use a blocking executor when calling from async code.