http://qs321.pair.com?node_id=884176

sans-clue has asked for the wisdom of the Perl Monks concerning the following question:

I have searched first, yes. I wish not to pop out to shell (to perform a netstat -an function) in order to glean the remote ip address that is connecting to my simple listener
use IO::Socket; my $sock = new IO::Socket::INET ( LocalHost => 'hosty', LocalPort => '6969', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; my $new_sock = $sock->accept(); while(<$new_sock>) { print $_; } close($sock);
Is there a simple way to get the connecting ip ? I need to look it up in a table to determine what to do next. I currently have a cludgey `netstat -an |grep 6969|grep ESTAB.....` step in there that I'd like to rid of. Thanks

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

    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.
      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.
        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.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: IO Socket - Detect inbound IP
by ikegami (Patriarch) on Jan 25, 2011 at 17:02 UTC
Re: IO Socket - Detect inbound IP
by rowdog (Curate) on Jan 26, 2011 at 00:48 UTC

    As Ikegami pointed out, the easy way is peerhost. Try it like this...

    while(<$new_sock>) { print $new_sock->peerhost, " said $_"; }