读取从 PHP 中的套接字接收的所有数据
我正在编写一个简单的 telnet 客户端库。 我使用 fsockopen() 打开套接字,并使用 fgetc() 和 fwrite() 读取和写入套接字。 我使用 fgetc() 而不是 fread() 来处理 Telnet 特殊命令和选项,例如(IAC,...)。
问题是大多数时候我从套接字读取数据,需要很长时间才能返回。 正如我所描述的,只要连接超时,就会花费很长的时间,之后我就会得到结果。
这是我的读取方法:
protected function getChar()
{
$char = fgetc( $this->_socket );
return $char;
} // public function getChar()
目前我使用此条件来确保流已完成:
$char = $this->getChar();
if ( $char === false ) {
// end of the stream.
}
还有其他方法可以确定流已完成并且所有数据都已读取吗?
Udate:我认为 Telnet 协议中的 EOF 字符不是 PHP 所期望的 EOF,这就是脚本找不到流结尾的原因。 有谁知道 Telnet 协议中的 EOF(文件结束)字符是什么?
I'm writing a simple telnet client library. I open sockets using fsockopen() and read and write to the socket using fgetc() and fwrite(). I use fgetc() instead of fread() to handle Telnet special commands and options like (IAC, ...).
the problem is that most of times I'm reading from the socket, it takes too long to return. as I have profiled it, it takes as long as the connection times out, and after that I have the results.
here are my reading method:
protected function getChar()
{
$char = fgetc( $this->_socket );
return $char;
} // public function getChar()
currently I use this condition to make sure stream is finished:
$char = $this->getChar();
if ( $char === false ) {
// end of the stream.
}
is there any other way to find out that the stream is finished an all data is read?
Udate: I think the EOF character in Telnet protocol is not what PHP expects as EOF, and that's why the script can not find end of the stream. does anyone know what is the EOF (end of file) character in Telnet protocol?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Telnet 没有流结束标记(字符)。
它只是发出提示并停止发送。
因此,最好的方法是将输出流捕获在提示文本上。
Telnet does not have a end of stream token (char).
It just sends out the prompt and stops sending.
So the best way is to trap your output stream on the prompt text.
尝试回显您收到的每个字符的十六进制值。 您应该能够看到您收到的 EOF 内容,即使 PHP 无法识别它。 查看 ord() 和 bin2hex()
Try echoing out the hex values of each character you receive. You should be able to see just what you are receiving as EOF, even if PHP doesn't recognize it as such. Look into ord() and bin2hex()
我搜索了一下,没有发现任何有用的东西。 然后我试图找出python中的telnetlib对这个问题做了什么(我刚刚学习python),我发现即使python库也不搜索EOF。 它只是继续读取流,直到超时,或者经过一定时间,或者搜索特殊字符串返回结果。 所以我用同样的方法来阅读并解决了我的问题。 感谢所有的答案和评论。 我将尝试说服我的公司使用开源兼容许可证分发此 telnet 库。 如果发生这种情况,我会在这里更新。
I searched and did not find anything useful. then I tried to find out what the telnetlib in python does for this problem (I'm just learning python), and I found out that even the python library does not search for EOF. it just keeps reading the stream until it times out, or a certain amount of time passes, or search for a special string returns a result. so I used the same method for reading and solved my problem. thanks for all the answers and comments. I'll try to convince my company to distribute this telnet library with an open source compatible license. if this happened, I'll update here.
有
feof()
可能有用。There is
feof()
that may be useful.