Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Perl network programming

by Viki@Stag (Sexton)
on Oct 03, 2007 at 06:17 UTC ( #642313=perlquestion: print w/replies, xml ) Need Help??
Viki@Stag has asked for the wisdom of the Perl Monks concerning the following question:

I have a (server side) script that waits_for_messages in an infinite loop. When i run this script i get an error: Bad arg length for Socket::pack_sockaddr_in, length is 0, should be 4 at /usr/lib/perl5/5.8.5/i386-linux-thread-multi/Socket.pm line 373 and the script exits. Please tell what this means. And how to come out of this error. Thanks in Advance, Viki

Replies are listed 'Best First'.
Re: Perl network programming
by naikonta (Curate) on Oct 03, 2007 at 07:41 UTC
    It's hard to tell exactly since you didn't show us your code. But I guess that you pass in undefined value or empty value (the second arg for IP) to the Socket::pack_sockaddr_in function. You may need to say,
    my $port = 8080; my $ip = '192.168.1.1'; my $sockaddr = pack_sockaddr_in($port, inet_aton($ip));

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Perl network programming
by jbert (Priest) on Oct 03, 2007 at 07:46 UTC
    On the face of it, a function (pack_sockaddr_in) is being called with something with length 0, instead of length 4. Given the name of the function and the fact that it's expecting a '4', I'd guess that it wants an IP address.

    So my best guess is that you're running a script which gets an IP address from somewhere (perhaps on the command line as an option, perhaps from a config file, perhaps it queries the system to find one) and this isn't happening, so the variable which normally contains the IP address is empty - causing the error.

    So you might want to check whatever documentation you have for this script (or read the source) to see how it gets the IP address it listens on. You might also want to check that if the box has any additional IP addresses, they are all present.

    Of course, this is all supposition. For trouble-shooting, some useful things to know are:

    • Did it use to work? (I guess it did?)
    • If it used to work, what has changed on that system since it did work? In particular anything related to network names or addresses.
    Good luck.

    p.s. The comments boxes on perlmonks take HTML, not plain text. Amongst other things, this means that your comment appears on one long line (and is harder to read) unless you break it up with <p> or <br> tags. You can use the 'Preview' button to see how others will see your comment.

      Thank you all for the reply.

      Actually i read a configuration file for the IP address. I have tried running the same code inside a private LAN (all machines having private IPs) where it worked fine. But when i try it using public IPs it shows this error.

      Is that scenario causing this error, or is it faulting in reading the config file.
        My money's on the config file. Perhaps a permissions problem?

        If so, the script could probably benefit from better error detection. If an IP address is required (as it seems to be) the code should validate that it has an IP address early on, and give a more informative error message.

        Some tools for chasing down the problem:

        • use 'strace' to see what the script does when it runs. If it fails to open() a config file, you'll see an error code (e.g. EPERM) which tells you why. The output of strace is voluminous, but you can send it to a file and grep it.
        • add some debugging to your script. log the value of the ip address at different stages of the processing.
        • make sure your script is handling errors correctly (a good way is to just 'die' if an unrecoverable problem occurs. e.g:
          my $cfg_file = "/my/config/file"; open(my $fh, "<", $cfg_file) or die "can't read config file [$cfg_file] for reading: $!";
          Note that we putting $! in your error handling gives you the reason why the open failed, which may help you track down the problem.
        As others have noted, I'm speculating wildly because we can't see your code. On the other hand, if your script is large it may not be useful to see all of it.

        If you can try reducing your script to the minimum amount which reproduces the problem that would be useful. You'll also often find that doing that helps you find the problem yourself.

        Good luck.

        It might be caused by the phase of the moon... we don't know without seeing some code.

        -David

Re: Perl network programming
by erroneousBollock (Curate) on Oct 03, 2007 at 07:44 UTC
    You'll likely get much better (or some) answers to your question if you post the code that isn't working.

    Please remember to show only as much code as is needed to replicate the problem. Also, have a look at Writeup Formatting Tips to see how to post code effectively.

    -David

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://642313]
Approved by lidden
help
Chatterbox?
and snow settles gently...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2017-06-26 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (585 votes). Check out past polls.