hi,

i use the following script to telnet in to a networked digital music player to issue commands. but it hasn't worked out as i have hope.

1   $roku_ip = "192.168.1.155";
2   $roku_port = 4444;
3   $command = "POWER_ON";

4   $fp = fsockopen($roku_ip, $roku_port, $errno, $errstr, 2);
6   if (!$fp) {echo "$errstr ($errno)<br>";}
7   else {
8       fputs($fp, "mmc\r\n");
9       fputs($fp, "IrDispatchCommand CK_" . $command . "\r\n");
10      fputs($fp, "GetDisplayData\r\n");

11      stream_set_timeout($fp,1);
12      while (!feof($fp)) {
13          $output = fread($fp,1024);
14          echo $output . "<br>";
15      }

16      fputs($fp, "exit\r\n");
17      fclose($fp);
18  }
19  // now do some other things such as
20  $output = substr($output, 10, 20);
21  echo "<br>printing out the final output<br>" . $output;

this script is supposed to do the following:
1. open up a telnet connection to the device.
2. open up the command shell ("mmc")
3. issue the command ("IrDispatchCommand CK_POWER_ON")
4. issue another command ("GetDisplayData")
5. read the response to the previous command
6. send the response to the browser
7. exit the shell
8. close the telnet connection
9. do some more processing
10. send the final result to the browser

as i was working through this, i found that there several problems.

first, for some reason, the commands i send in (lines 8 thru 10) do not execute if the block to read the resulting output (lines 11 thru 15) are commented out. not really a big issue, because i do want the resulting output. but i am very curious as to why the commands would not execute alone. am i missing a trigger of some sort?

second, it seems as if the script stops right after line 14. it never continues beyond that. only if i comment out the block to read the response (lines 11 thru 15), does the script continue till the end. but without the block to read the output, the commands sent in (lines 8 thru 10) aren't executed. i'm reminded of the chicken and the egg problem.

third, despite the fact that i had set the timeout on the stream (line 11), it still takes just as long for the output to appear on the browser. is there a faster way to read out the response. when i telnet in directly, the response is almost intantaneous. why does it take as long as 30 seconds for it to appear on the browser? is there another method of reading the response other than "feof" that would be faster?

any ideas at all would be appreciated.

    6 days later

    i found this thread unanswered from about 4 months ago.

    it's similar to what i'm facing in my own post.

    in fact, all posts that deal with fsockopen() and telnet i've found are all unresolved.
    is this because fsockopen() is not meant to be used for telnet?

      Write a Reply...