getpeername
Prototype:
#include <sys/socket.h>
int getpeername(int sockfd, struct sockaddr *addr, int *addr_len);
General Description: This routine gets the bound address or name of connected peer at the other end of the sockfd channel. The call places the results in buf. The buf_len is the number of bytes available in buf. If too small, the information gets truncated. The is the same information you get with the accept() call.
Return Value: Zero (0) if everything goes well. If an error occurs, you can find the cause in errno.
Parameters
sockfd The connected socket channel.
addr A buffer that holds the address structure.
addr_len The number of bytes available in addr. This is pass by reference (the call changes this field).
Possible Errors
EBADF The argument sockfd is not a valid descriptor.
ENOTSOCK The argument sockfd is a file, not a socket.
ENOTCONN The socket is not connected.
ENOBUFS Insufficient resources were available in the system to perform the operation.
EFAULT The addr parameter points to memory not in a valid part of the process address space.
Examples
struct sockaddr_in addr;
int add_len = sizeof(addr);
if ( getpeername(client, &addr, &addr_len) != 0 )
    perror("getpeername() failed");
printf("Peer: %s:%d\n", inet_ntoa(addr.sin_addr),
    ntohs(addr.sin_port));