PHP 套接字 - 不读取输出
我在从套接字读取数据时遇到一些问题。基本上我可以连接到机器,没有错误。我可以毫无错误地写入机器。我似乎无法读取输出。
这就是我正在做的事情:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我查看了“阻止”值并发现了问题。
本质上,当阻塞== true时,如上所述,返回值将等待变得可用。设置为解锁意味着该值将立即返回。
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.