Home

addrlen

addrlen is a conventional variable name used in socket programming to denote the length of a socket address structure, typically a value used with operations involving storing or providing a socket address. In many APIs, the address is stored in a struct such as sockaddr, sockaddr_in, or sockaddr_in6, and addrlen specifies the size of that structure.

In POSIX systems, the length is typically of type socklen_t. In Windows Sockets, the corresponding parameter

Best practices include initializing addrlen to the size of the buffer you pass (e.g., sizeof(struct sockaddr_storage))

Using portable types like sockaddr_storage for temporary buffers, and socklen_t for the length when possible, helps

See also socklen_t, sockaddr, getsockname, getpeername, accept, sendto, recvfrom.

is
an
int.
The
addrlen
parameter
is
usually
used
as
an
in/out
value.
For
example,
in
accept,
you
provide
the
address
buffer
and
its
length;
on
return,
addrlen
contains
the
actual
length
of
the
peer
address.
In
getsockname
and
getpeername,
you
pass
a
pointer
to
addrlen
to
receive
the
length
of
the
returned
address.
In
sendto
and
recvfrom,
you
pass
the
length
of
the
destination
or
source
address.
and
checking
that
the
function
succeeds,
then
using
the
returned
length
to
interpret
the
address.
If
the
buffer
is
too
small,
the
call
fails
and
may
set
errno
(or
a
Windows
error).
maintain
cross-platform
compatibility.