ChopperCharles has asked for the wisdom of the Perl Monks concerning the following question:
I'm building a TCP client to run RPC commands on an existing server. Everything was going fine, connecting, sending, receiving, all of that worked. UNTIL I tried to run some tight code.
A single RPC command is:
Send command. Receive "command completed execution" response. Send "hey what were my results" Receive results from console.
My issue is that right now I'm trying to create a directory on the target: "/hello/world/goodbye/happiness". I cd to each directory one by one, until I receive an error, and from that point on I create the directories.
Here's my senddata:
sub sendData { my $request = $_[0]; $responseData = ""; #class member var print SOCKET "$request\n" . "\000" or die "print SOCKET: $!"; $responseData = <SOCKET>; $responseData =~ tr/\x80-\xFF//d; #strip non printing chars > 127 $responseData =~ tr/\x00-\x08//d; #strip non printing chars > 0-8 print "resp: $responseData\n"; } sub sendCommand{ my $command = $_[0]; $command .= "\n"; #terminate command with newline. $responseData = ""; # member var $commandResponse = ""; # member var sendData(BuildWriteXML($command)); checkError($responseData); #make sure above was successful. $responseData = ""; sendData(BuildReadXML()); #retrieve response to above command checkError($responseData); $commandResponse = $responseData; }
Here's a snippet of code for walking through the directory tree:
my @destArray = split(/\//, $destPath); #splits path on the forward + slash. while(scalar(@destArray)) { my $cmd = "cd \"" . $destArray[0] . "\""; print "command: $cmd\n"; sendCommand($session, $cmd);#"cd \"" . $destArray[0] . "\""); if ($commandResponse =~ m/Operation failed/) { print "Debug: last cd command failed, so now create the rest of +the tree.\n"; last; } shift (@destArray); # otherwise remove the directory from the lis +t and continue }
So, I want to CD into "/hello/world/goodbye/happiness". Right now, only "/hello/world" exists. I'm expecting:
command: cd hello response: [ok] response: hello command: cd world response: [ok] response: world command: cd goodbye response: [ok] response: Operation failed.
Instead, I'm getting:
command: cd hello command: cd world response: command: cd goodbye response: [ok] response: response: command: cd happiness response: [ok] response: [ok] response: world response: hello response: [ok] response: Operation failed. response: Operation failed.
Any attempt to wait for data fails or blocks indefinitely. I can even put checks in there to see if the $responseData; contains the correct data... and that test PASSES even if the stuff printed to the screen is horribly wrong.
However, if I put a usleep(250000); #250ms in my sendData function, it all works. Is there a better way to do this? This is just a hack, and I know that.
What I want is the ability to do something like this:
while(responseData.indexOf("\n") == -1) { DoEvents(); } (excuse the ps +eudo code) or something, to poll the response and wait until a respon +se has actually been received.
Note all my code examples above have been simplified for the sake of readability and understanding and not taking up the whole goram page.
Charles.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: TCP client buffering problems
by BrowserUk (Patriarch) on Oct 14, 2011 at 01:57 UTC | |
|
Re: TCP client buffering problems
by bluescreen (Friar) on Oct 13, 2011 at 23:38 UTC |