views:

157

answers:

1

Hi all, I'm having some problems reading from a socket. Basically, I can connect to the machine without an error. I can write to the machine without an error. I cannot seem to read the output.

Here is what I'm doing:

$header1 = chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);
$header2 = chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);

    $fp = pfsockopen('ServerName',$port,$errno,$errstr,35);
    if(!$fp) {
        echo '<p>Error connecting to system.</p>';
        echo $errstr . ' (' . $errno . ')';
    } else {
        fputs($fp, $header1);
        sleep(1);
        fputs($fp, $header2);
        sleep(1);
        fputs($fp, "99\r");
        sleep(3);

        $read = fread($fp, 128);
        $pos = socket_get_status($fp);
        echo '<pre>';
        var_dump($pos);
        echo '</pre>';
        //$read = fread($fp,$pos["unread_bytes"]);
        //$read = str_replace("\n","<br>",$read);
        //$explode = explode("\"",$read);
    }
    fclose($fp);

What is really weird to me is that the socket_get_status() has unread bytes of 0, yet EOF is false. Seems fairly contradictory to me.

array(7) {
  ["stream_type"]=>
  string(14) "tcp_socket/ssl"
  ["mode"]=>
  string(2) "r+"
  ["unread_bytes"]=>
  int(0)
  ["seekable"]=>
  bool(false)
  ["timed_out"]=>
  bool(false)
  ["blocked"]=>
  bool(true)
  ["eof"]=>
  bool(false)
}

Does anyone have any ideas why I can't get the output?

Thanks!

A: 

I looked into the 'blocked' value and found the problem.

Essentially, when blocked == true, as above, then the return value will wait to become available. Setting it to unblocked means the value will be returned right away.

stream_set_blocking($fp, 0);
Cory Dee