getpeername(2)
๐ getpeername(2) โ bestie's guide to figuring out who you're talking to ๐
okay so real talk, this is getpeername(2) โ a Linux system call manual page, and it's genuinely one of the most "who even is that" functions in the whole socket family. if you've ever been on a call and thought "...and you are??" โ that's this. it's the caller ID of the socket world. no cap. ๐ซถ
โจ NAME โจ
getpeername โ get the name of the connected peer socket ๐
๐ LIBRARY ๐
Standard C library (libc, -lc) ๐
๐ฆ SYNOPSIS ๐ฆ
#include <sys/socket.h>
int getpeername(int sockfd, struct sockaddr *restrict addr,
socklen_t *restrict addrlen);
๐ธ DESCRIPTION ๐ธ
so here's the vibe. getpeername() hands you back the address of the peer that's connected to the socket sockfd, and it drops that info into the buffer pointed to by addr. โจ
the addrlen argument is a little diva with two jobs, bestie. you gotta initialize it to say how much space addr has. on return, it holds the actual size of the name that came back โ in bytes. and if your buffer was too smol? the name gets truncated. simple as that. ๐
and yeah, they said it again, so here it is again: the returned address is truncated if the buffer you provided is too small โ and in that case, addrlen will return a value greater than the value you supplied to the call. so if addrlen comes back bigger than you sent it in? that's not a glitch, that's the function telling you to get a bigger buffer. ๐
๐ฅ RETURN VALUE ๐ฅ
- on success:
0(iconic, flawless, no notes) ๐ - on error:
-1, anderrnogets set to tell you what went wrong ๐ญ
๐ ERRORS ๐
okay so when it's not slaying, here's what errno might be telling you:
EBADFโ the argumentsockfdis not a valid file descriptor. miss girl, that's not even a real socket, pls ๐ซEFAULTโ theaddrargument points to memory that's not in a valid part of the process address space. that pointer is lost, bestie ๐ซฅEINVALโaddrlenis invalid (like, it's negative). negative?? a length?? ma'am ๐ฅดENOBUFSโ there weren't enough resources available in the system to perform the operation. the system is booked and busy, try again later ๐ENOTCONNโ the socket is not connected. you can't get the name of a peer that doesn't exist, babes ๐ENOTSOCKโ the file descriptorsockfddoes not refer to a socket. close but so far off ๐
โญ STANDARDS โญ
POSIX.1-2024 โ certified, official, standards-approved royalty ๐
๐ HISTORY ๐
POSIX.1-2001, SVr4, 4.4BSD โ and it first appeared in 4.2BSD. so yeah, this girl's been around since the 80s and she's still relevant. main character energy fr fr ๐
๐ง NOTES ๐ง
this is the part where things get kind of nuanced, so listen up:
- for stream sockets, once a
connect(2)has been performed, either socket can callgetpeername()to get the address of the peer socket. mutual info, mutual access, we love reciprocity ๐ - datagram sockets, though? they're connectionless. periodt. calling
connect(2)on a datagram socket merely sets the peer address for outgoing datagrams sent withwrite(2)orrecv(2). it's like saving a contact, not making a call ๐ฑ - the caller of
connect(2)can usegetpeername()to grab the peer address it earlier set for the socket. cool, exceptโฆ - the peer socket is unaware of any of this. so if you call
getpeername()on the peer socket, you'll get no useful information โ unless aconnect(2)call was also executed on that peer. the info doesn't teleport, bestie ๐ซ - also note: the receiver of a datagram can get the address of the sender by using
recvfrom(2). that's the move if you need it ๐
๐ SEE ALSO ๐
accept(2), bind(2), getsockname(2), ip(7), socket(7), unix(7) ๐
okay that's a wrap!! this page is from Linux man-pages 6.20, dated 2025-10-29 โ so you know it's fresh, you know it's accurate, you know it ate. now go call getpeername() on your sockets and find out who's on the other end ๐
โจ๐ฆ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: BSD-4-Clause-UC
- Copyright 1983-1991, The Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms