Link to home
Start Free TrialLog in
Avatar of jhill03
jhill03

asked on

Error when trying to collect hostname/ip address by using sockaddr_in()

Running into an error when trying to collect the hostname/ip address of a client app.
The error is:
Bad arg length for Socket::unpack_sockaddr_in, length is 32, should be 16 at /usr/lib/perl5/5.8.5/i386-linux-thread-multi/Socket.pm line 370.


The sever side code is as follows:
-------------------------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/perl -w

use strict;
use IO::Socket;
use Data::Dumper;


# port to connect to
my $server_port = 7890;

my $server = IO::Socket::INET->new( LocalPort => $server_port,
                                    Type      => SOCK_STREAM,
                                    Reuse     => 1,
                                    Listen    => 10 )
        or die "Couldn't be tcp server on port $server_port: $@\n";

while ( my ($client,$client_address)  = $server->accept() ) {
  my ($client_ip, $c_ip) = sockaddr_in($client);
  my $client_ipnum = inet_ntoa($c_ip);
  my $client_host = gethostbyaddr($c_ip, AF_INET);
print "got a connection from: $client_host " ,
      "[$client_ipnum]\n";

}

close($server);

---------------------------------------------------------------------------------------------------------------------------------------------------------

When I Data::Dumper on $client_address, it gives me:
dumping $client_address: $VAR1 = 'ýK~!';
Don't know if that means anything or if it is just weirdness my terminal can't handle...

This is my first journey into socket programming with PERL, and I haven't done any socket programming in years... so while I'm pretty sure it is probably a simple fix (at least I hope so) it isn't jumping out at me right now.

Versions:
IO::Socket::VERSION is 1.30
O/S: RHEL 4
perl: 5.8.5
ASKER CERTIFIED SOLUTION
Avatar of RichieHindle
RichieHindle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial