Gets the name of the peer socket.
#include <sys/socket.h>
int getpeername ( Socket, Name, NameLength)
int Socket;
struct sockaddr *Name;
socklen_t *NameLength;
The getpeername subroutine retrieves the Name parameter from the peer socket connected to the specified socket. The Name parameter contains the address of the peer socket upon successful completion.
A process created by another process can inherit open sockets. The created process may need to identify the addresses of the sockets it has inherited. The getpeername subroutine allows a process to retrieve the address of the peer socket at the remote end of the socket connection.
Note: The getpeername subroutine operates only on connected sockets.
A process can use the getsockname subroutine to retrieve the local address of a socket.
Upon successful completion, a value of 0 is returned and the Name parameter holds the address of the peer socket.
If the getpeername subroutine is unsuccessful, the system handler performs the following functions:
The getpeername
subroutine is unsuccessful if any of the following errors occurs:
The following program fragment illustrates the use of the getpeername subroutine to return the address of the peer connected on the other end of the socket:
struct sockaddr_in name; int namelen = sizeof(name); . . . if(getpeername(0,(struct sockaddr*)&name, &namelen)<0){ syslog(LOG_ERR,"getpeername: %m"); exit(1); } else syslog(LOG_INFO,"Connection from %s",inet_ntoa(name.sin_addr)); . . .
The getpeername subroutine is part of Base Operating System (BOS) Runtime.
All applications containing the getpeername subroutine must be compiled with _BSD set to a specific value. Acceptable values are 43 and 44. In addition, all socket applications must include the BSD libbsd.a library.
The accept subroutine, bind subroutine, getsockname subroutine, socket subroutine.
Sockets Overview in AIX 5L Version 5.1 Communications Programming Concepts.