getpeername
getpeername is a socket API function used to obtain the address of the remote peer to which a socket is connected. It is commonly used with stream sockets such as TCP and with connected datagram sockets to learn the peer’s network endpoint for logging, access control, or response routing.
Signature and parameters: In POSIX systems, int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen). The function
Address families: The returned address is a sockaddr structure of the appropriate family (AF_INET for IPv4,
When it can fail: The function returns 0 on success and -1 on error, with errno set.
Usage example: After accept() or connect(), call getpeername to retrieve the peer’s address and port, e.g., fill
See also: getsockname to obtain the local address, and getnameinfo to convert binary addresses to text.