Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2007-09-27
1
Medium Priority
?
969 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:jhill03
1 Comment
 
LVL 14

Accepted Solution

by:
RichieHindle earned 1400 total points
ID: 19982183
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question