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, and errno gets 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 argument sockfd is not a valid file descriptor. miss girl, that's not even a real socket, pls ๐Ÿšซ
  • EFAULT โ€” the addr argument points to memory that's not in a valid part of the process address space. that pointer is lost, bestie ๐Ÿซฅ
  • EINVAL โ€” addrlen is 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 descriptor sockfd does 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 call getpeername() 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 with write(2) or recv(2). it's like saving a contact, not making a call ๐Ÿ“ฑ
  • the caller of connect(2) can use getpeername() 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 a connect(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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms