in reply to Re: Getting client ip address from socket connect
in thread Getting client ip address from socket connect

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

Replies are listed 'Best First'.
Re^3: Getting client ip address from socket connect
by minixman (Beadle) on Nov 01, 2005 at 14:45 UTC
    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