1getpeername(2) System Calls Manual getpeername(2)
2
3
4
6 getpeername - get name of connected peer socket
7
9 Standard C library (libc, -lc)
10
12 #include <sys/socket.h>
13
14 int getpeername(int sockfd, struct sockaddr *restrict addr,
15 socklen_t *restrict addrlen);
16
18 getpeername() returns the address of the peer connected to the socket
19 sockfd, in the buffer pointed to by addr. The addrlen argument should
20 be initialized to indicate the amount of space pointed to by addr. On
21 return it contains the actual size of the name returned (in bytes).
22 The name is truncated if the buffer provided is too small.
23
24 The returned address is truncated if the buffer provided is too small;
25 in this case, addrlen will return a value greater than was supplied to
26 the call.
27
29 On success, zero is returned. On error, -1 is returned, and errno is
30 set to indicate the error.
31
33 EBADF The argument sockfd is not a valid file descriptor.
34
35 EFAULT The addr argument points to memory not in a valid part of the
36 process address space.
37
38 EINVAL addrlen is invalid (e.g., is negative).
39
40 ENOBUFS
41 Insufficient resources were available in the system to perform
42 the operation.
43
44 ENOTCONN
45 The socket is not connected.
46
47 ENOTSOCK
48 The file descriptor sockfd does not refer to a socket.
49
51 POSIX.1-2008.
52
54 POSIX.1-2001, SVr4, 4.4BSD (first appeared in 4.2BSD).
55
57 For stream sockets, once a connect(2) has been performed, either socket
58 can call getpeername() to obtain the address of the peer socket. On
59 the other hand, datagram sockets are connectionless. Calling con‐
60 nect(2) on a datagram socket merely sets the peer address for outgoing
61 datagrams sent with write(2) or recv(2). The caller of connect(2) can
62 use getpeername() to obtain the peer address that it earlier set for
63 the socket. However, the peer socket is unaware of this information,
64 and calling getpeername() on the peer socket will return no useful in‐
65 formation (unless a connect(2) call was also executed on the peer).
66 Note also that the receiver of a datagram can obtain the address of the
67 sender when using recvfrom(2).
68
70 accept(2), bind(2), getsockname(2), ip(7), socket(7), unix(7)
71
72
73
74Linux man-pages 6.05 2023-04-03 getpeername(2)