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.


In reply to TCP client buffering problems by ChopperCharles

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.