PHP 套接字 - 不读取输出

发布于 2024-08-06 17:44:48 字数 2018 浏览 9 评论 0原文

我在从套接字读取数据时遇到一些问题。基本上我可以连接到机器,没有错误。我可以毫无错误地写入机器。我似乎无法读取输出。

这就是我正在做的事情:

$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);

对我来说真正奇怪的是 socket_get_status() 的未读字节为 0,但 EOF 为 false。对我来说似乎相当矛盾。

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)
}

有人知道为什么我无法获得输出吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄与心 2024-08-13 17:44:48

我查看了“阻止”值并发现了问题。

本质上,当阻塞== true时,如上所述,返回值将等待变得可用。设置为解锁意味着该值将立即返回。

stream_set_blocking($fp, 0);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文