in reply to Getting client ip address from socket connect

Hi minixman,

"All i have some code..."
Where is this code, you have not provided much info to go on.
If you are using IO::Socket::INET have a look at the peeraddr() method:
"Return the address part of the sockaddr structure for the socket on the peer host"

This is from the documentation.

Hope this helps.

Martin

Replies are listed 'Best First'.
Re^2: Getting client ip address from socket connect
by tirwhan (Abbot) on Nov 01, 2005 at 12:57 UTC

    Or even the peerhost() method if you want the IP address directly in the usual dotted-quad(x.x.x.x) notation.


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
      sorry
      using use IO::Socket::INET;

      my $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $serverport, Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; my $remotehost; &writelog("$date: INFO: Server $0 started accepting clients connection +s"); while ($client = $server->accept()) { $client->autoflush(1); $remotehost = $client->peeraddr; } But i get this in the log 01/11/2005/ 14:42: INFO: New client connection from Μφ

        Yes, that's because peeraddr() returns the raw in_addr struct. Either convert it to a string with

        $remotehost=inet_ntoa($client->peeraddr);

        or use

        $remotehost=$client->peerhost;
        like I suggested in my node.

        Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
        One thing that I would change is the die unless that you have, you could probably rewrite that to this:
        my $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $serverport, Listen => SOMAXCONN, Reuse => 1) or die("Can't setup server: $!");

        I'm not 100% sure that that will work though, I haven't really gone near IO::Socket.
        Spidy