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

Greetings oh wise ones. I'm working on a simple client/server script that just talks back and forth, but I'm having trouble with the script not processing my if statements after data is sent to it. I will then however continue on after more data is sent and stop again at the same place. The ####Problem here.#### is where the server stops after I send it the first bit of information. Here is the code minus some of the basic socket stuff which I omitted.

Server
while(1){ $MySocket->recv($text,2); if($text ne '') { print "\n Number'", $text,"'\n","\n"; ####Problem here.#### if($text=~m/\d.*/){ print "\nThat's a number"; }else{ print "\nNot a number"; } }else{ print "Cilent has exited!"; exit 1; } }
Client
print "\n",$def_msg; while($msg=<STDIN>) { chomp $msg; if($msg ne '') { #print "\nSending message '",$msg,"'"; if($MySocket->send($msg)){ print ".....<done>","\n"; print $def_msg; } } else { # Send an empty message to server and exit $MySocket->send(''); exit 1; } }

After the server reads in the input it runs the print statement with the:

print "\n Number'", $text,"'\n","\n";

But it won't run the if statement after it until I sent another piece of data from the client E.G. I fire open the client and type in the number 1 then hit enter. My server prints this output:

>> Server Program << Number'1'

And stops there until I enter something else on my client; say I enter the number 2. My server now looks like this:

>> Server Program << Number'1' That's a number Number'2'
So it moved on to the if statement only after seeing more input from the client. Any ideas?

Replies are listed 'Best First'.
Re: IO Socket INET stopping in middle of script
by ikegami (Patriarch) on Oct 29, 2009 at 22:52 UTC
    You are suffering from buffering. Your server's output to STDOUT isn't getting flushed due to your weird placement of the newlines.

      Thank you ikegami. I added $|=1 to my code before the first print and it's working correctly now. <\p>