in reply to Re: IO Socket - Detect inbound IP
in thread IO Socket - Detect inbound IP

no luck, it returned binary garbage that once converted to ascii read 512.

Replies are listed 'Best First'.
Re^3: IO Socket - Detect inbound IP
by BrowserUk (Patriarch) on Jan 25, 2011 at 21:19 UTC

    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
Re^3: IO Socket - Detect inbound IP
by Anonymous Monk on Jan 25, 2011 at 19:31 UTC
    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

Re^3: IO Socket - Detect inbound IP
by ikegami (Patriarch) on Jan 25, 2011 at 19:34 UTC

    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);

        $sock isn't connected to anything. That's why you're getting

        Use of uninitialized value in subroutine entry Bad arg length for Socket::inet_ntoa, length is 0, should be 4

        $! contains the following, but you might not have known that peeraddr sets it on error.

        Transport endpoint is not connected

        You want to know to whom $new_sock is connected.

    A reply falls below the community's threshold of quality. You may see it by logging in.