jobinjoseph121 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a perl script which uses socket calls in perl.
Server Side:
use Socket; my $port = shift || 7001; my $proto = getprotobyname('tcp'); my $server = "localhost"; socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "Can't open socket + $!\n"; setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1) or die "Can't set sock +et option to SO_REUSEADDR $!\n"; bind( SOCKET, pack_sockaddr_in($port, inet_aton($server))) or die "Can +'t bind to port $port! \n"; listen(SOCKET, 5) or die "listen: $!"; my $client_addr; while ($client_addr = accept(NEW_SOCKET, SOCKET)) { send(NEW_SOCKET,"Hello",0); close NEW_SOCKET; }

Client Side:
use Socket; my $host = shift || 'localhost'; my $port = shift || 7001; my $server = "localhost"; socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2]) or die " +Can't create a socket $!\n"; connect( SOCKET, pack_sockaddr_in($port, inet_aton($server))) or die " +Can't connect to port $port! \n"; my $line; my $val; my $p; my $iaddr; $line = recv(SOCKET,$val,5,0); ($p, $iaddr) = sockaddr_in($line); if(defined $line){ print "$val from $iaddr\n"; } close SOCKET or die "close: $!";

The problem happens only when i use the statement  ($p, $iaddr) = sockaddr_in($line);. The recv call receives the message "Hello". If the statement ($p, $iaddr) = sockaddr_in($line); is removed, the script receives the correct value in recv and will print the same. Hence the problem is only with the return value in $line of recv which seems to be 'undef' in this case.

Could you please help to identify the issue here?

Thank you!
  • Comment on Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be 16 at Socket.pm line 780
  • Select or Download Code

Replies are listed 'Best First'.
Re: Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be 16 at Socket.pm line 780
by Anonymous Monk on Sep 10, 2015 at 05:39 UTC

    Could you please help to identify the issue here?

    You're not error checking ("or die...")

    :) Why not use something slightly higher up the food chain, say IO::Socket, then you can

    =head2 $addr = $sock->peerhost Return the numeric form of the peer address as a textual representatio +n =head2 $port = $sock->peerport Return the numeric form of the peer port number
      Hi,

      Thank you for the reply.

      Unfortunately i can not switch to IO::Socket but had to go with Socket option only.
      Can you please suggest any method to correct this issue?

      Thanks and Regards,
      Jobin Joseph

        Unfortunately i can not switch to IO::Socket but had to go with Socket option only. Can you please suggest any method to correct this issue?

        :) IO::Socket is a core module just like Socket -- you can use both

        At the very least try using IO::Socket to see if you can make it work ... then work back to see if you can figure out where you go wrong

Re: Bad arg length for Socket::unpack_sockaddr_in, length is 0, should be 16 at Socket.pm line 780
by Anonymous Monk on Sep 10, 2015 at 08:14 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.