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

    I seriously doubt you will get a helpful response to your question. What you have posted is far to "mocked up" and "simplified" for anyone to be able to guess what you are actually doing.

    Simple example. You say:

    Instead, I'm getting: command: cd hello command: cd world response: command: cd goodbye ...

    But looking at the snippets you've posted, the first thing I notice is that the print statement that would be producing that response: trace is   print "resp: $responseData\n";, making your "actual output" nothing of the kind.

    In addition, the logic of your sub:

    sub sendData { my $request = $_[0]; $responseData = ""; #class member var print SOCKET "$request\n" . "\000" or die "print SOCKET: $!"; $responseData = <SOCKET>; ... print "resp: $responseData\n"; }

    is such that there should be no way for your sends and receives to get out of sync as you follow each transmission with a blocking read.

    Unless of course you have set the socket to be non-blocking, in which case, you should not be using the line-oriented diamond operator.

    Add to that this line:

    $responseData = ""; #class member var

    which suggests that you think you are writing OO code but obviously are not, and that you are relying upon globals vars to convey data between subroutines, and it suggests that you have one unholy mess on your hands.


    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.
Re: TCP client buffering problems
by bluescreen (Friar) on Oct 13, 2011 at 23:38 UTC

    I will recommend you to look at AnyEvent it allows you to do other stuff while waiting for a data to come