1GETPEERNAME(2)             Linux Programmer's Manual            GETPEERNAME(2)
2
3
4

NAME

6       getpeername - get name of connected peer socket
7

SYNOPSIS

9       #include <sys/socket.h>
10
11       int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
12

DESCRIPTION

14       getpeername()  returns  the address of the peer connected to the socket
15       sockfd, in the buffer pointed to by addr.  The addrlen argument  should
16       be  initialized to indicate the amount of space pointed to by addr.  On
17       return it contains the actual size of the  name  returned  (in  bytes).
18       The name is truncated if the buffer provided is too small.
19
20       The  returned address is truncated if the buffer provided is too small;
21       in this case, addrlen will return a value greater than was supplied  to
22       the call.
23

RETURN VALUE

25       On  success,  zero is returned.  On error, -1 is returned, and errno is
26       set appropriately.
27

ERRORS

29       EBADF  The argument sockfd is not a valid file descriptor.
30
31       EFAULT The addr argument points to memory not in a valid  part  of  the
32              process address space.
33
34       EINVAL addrlen is invalid (e.g., is negative).
35
36       ENOBUFS
37              Insufficient  resources  were available in the system to perform
38              the operation.
39
40       ENOTCONN
41              The socket is not connected.
42
43       ENOTSOCK
44              The file descriptor sockfd does not refer to a socket.
45

CONFORMING TO

47       POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD (getpeername() first  appeared
48       in 4.2BSD).
49

NOTES

51       For background on the socklen_t type, see accept(2).
52
53       For stream sockets, once a connect(2) has been performed, either socket
54       can call getpeername() to obtain the address of the  peer  socket.   On
55       the  other  hand,  datagram  sockets  are connectionless.  Calling con‐
56       nect(2) on a datagram socket merely sets the peer address for  outgoing
57       datagrams  sent with write(2) or recv(2).  The caller of connect(2) can
58       use getpeername() to obtain the peer address that it  earlier  set  for
59       the  socket.   However, the peer socket is unaware of this information,
60       and calling getpeername() on the peer socket will return no useful  in‐
61       formation  (unless  a  connect(2)  call was also executed on the peer).
62       Note also that the receiver of a datagram can obtain the address of the
63       sender when using recvfrom(2).
64

SEE ALSO

66       accept(2), bind(2), getsockname(2), ip(7), socket(7), unix(7)
67

COLOPHON

69       This  page  is  part of release 5.10 of the Linux man-pages project.  A
70       description of the project, information about reporting bugs,  and  the
71       latest     version     of     this    page,    can    be    found    at
72       https://www.kernel.org/doc/man-pages/.
73
74
75
76Linux                             2017-09-15                    GETPEERNAME(2)
Impressum