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
jhill03Asked:
Who is Participating?
 
RichieHindleCommented:
Here you go:

#!/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_port, $client_ip) = sockaddr_in($client_address);  
  my $client_hostname = gethostbyaddr($client_ip, AF_INET);
  my $client_address_str = inet_ntoa($client_ip);
 
print "got a connection from: $client_hostname [$client_address_str]\n";

}

close($server);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.