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

Hi monks, I have two simple scripts, a TCP server and a TCP client. My server sends the client a random word (e.g Hello) upon EVERY CONNECTION... So my client has a for loop that connects to the server 10 times and requests a new word. After the seventh connection, everything stops working, and I get an error: "Use of uninitialized value $word in concatenation (.)" Here is my code:
[...] for (my $i = 0; $i < 10; $i++) { $socket->recv(my $word, 1024); print "Your new word is: $word\n"; } [...]
I am using warnings and strict in my code. Do I need the IO::Socket::INET in the for loop, or, whats happening? :(

NOTE: $socket is also declared in the first scope of code

Replies are listed 'Best First'.
Re: Getting values from server
by roboticus (Chancellor) on May 21, 2012 at 17:18 UTC

    DaveMonk:

    You don't show enough code for me to be sure, but here are a couple things:

    • You don't show a new connection attempt in your loop, so why would you expect the salutation more than once?
    • Are you certain that your sending script is actually sending something every time? Perhaps it's sending a null buffer ever seven tries or so?

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Getting values from server
by BrowserUk (Patriarch) on May 21, 2012 at 18:05 UTC

    I can think of a few possible reasons, and if you posted runnable code, we could eliminate some of the possibilities without having to play 20 questions.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Getting values from server
by zentara (Cardinal) on May 21, 2012 at 20:19 UTC
    After the seventh connection, everything stops working, and I get an error:

    Without seeing the server code, I would guess you don't have the Reuse or Listen options set to the proper values.

    my $server = IO::Socket::INET->new ( LocalAddr => '192.168.0.1', LocalPort => 1337, Type => SOCK_STREAM, Reuse => 1, Listen => 10 ) or die "could not open port\n"; warn "server ready waiting for connections..... \n";

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh