Perl 5 version 26.3 documentation

getsockname

  • getsockname SOCKET

    Returns the packed sockaddr address of this end of the SOCKET connection, in case you don't know the address because you have several different IPs that the connection might have come in on.

    1. use Socket;
    2. my $mysockaddr = getsockname($sock);
    3. my ($port, $myaddr) = sockaddr_in($mysockaddr);
    4. printf "Connect to %s [%s]\n",
    5. scalar gethostbyaddr($myaddr, AF_INET),
    6. inet_ntoa($myaddr);