in reply to IO Socket - Detect inbound IP

In a list context, the accept method also returns the peer address:

my( $client_sock, $client_addr ) = $sock->accept();

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: IO Socket - Detect inbound IP
by sans-clue (Beadle) on Jan 25, 2011 at 19:16 UTC
    no luck, it returned binary garbage that once converted to ascii read 512.

      The 'address' return by accept is actually a packed sockaddr_in struct.

      It can be unpacked using the functions sockaddr_in() and then the inet_ntoa() functions as shown in perlipc server example (which is linked from the perlfunc description of accept):

      for ( ; $paddr = accept(Client,Server); close Client) { my($port,$iaddr) = sockaddr_in($paddr); my $name = gethostbyaddr($iaddr,AF_INET); logmsg "connection from $name [", inet_ntoa($iaddr), "] at por +t $port";

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I will try this thanks
      no luck, it returned binary garbage that once converted to ascii read 512.

      I haven't checked the documentation, but binary garbage usually means a packed address, so to get dotted octet form (a.b.c.d), you need to use Socket::inet_ntoa

      You are mistaken. It does indeed work. Don't blame the tool if you don't understand its output.

      The address is returned in its packed form. Each character of the string is one of the octets. Pass it to inet_ntoa to convert it to the dotted address form. Aforementioned peerhost does this for you.

        here it is.
        my ($new_sock, $client_addr) = $sock->accept(); my $peerhost = $sock->peeraddr(); my $addr = inet_ntoa($peerhost); print "$addr\n"; print $client_addr\n"; while(<$new_sock>) { print $_; } close($sock);
      A reply falls below the community's threshold of quality. You may see it by logging in.