我对套接字的 fwrite 不会刷新,直到套接字关闭。如何改变?
我对套接字的 fwrite 不会刷新,直到套接字关闭。如何改变?我希望它在每次写入后刷新。
我尝试过:
1)flush()
2)fflush()
3)ob_implicit_flush(true);
这些都不起作用,我仍然必须退出 php 让我的套接字接收数据。
包括一些示例代码,看起来有什么问题吗?
while($clientSocket = socket_accept($this->serviceConnection))
{
while( $clientMessage = socket_read($clientSocket, 1024) )
{
echo 'Relaying message to server: ' . $clientMessage;
if( !fwrite($this->Connection, $clientMessage) )
echo 'Error writing to server';
fflush($this->Connection);
}
socket_close($clientSocket);
}
My fwrite to a socket is not flushed, until the socket closes. How to change? I want it to flush after each fwrite.
I've tried:
1) flush()
2) fflush()
3) ob_implicit_flush(true);
None of those worked, I still had to quit php for my socket to receive the data.
Including some sample code, anything looks wrong?
while($clientSocket = socket_accept($this->serviceConnection))
{
while( $clientMessage = socket_read($clientSocket, 1024) )
{
echo 'Relaying message to server: ' . $clientMessage;
if( !fwrite($this->Connection, $clientMessage) )
echo 'Error writing to server';
fflush($this->Connection);
}
socket_close($clientSocket);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个函数可以做到这一点! fflush($rc)
您确定您拥有正确的资源吗作为参数?
编辑
好的。查看您的代码,您正在使用 socket_* 函数。你需要使用fsockopen、fwrite、fflush、fsockclose。您的代码应该工作相同,但您将能够刷新输出。查看 php 手册中的示例了解更多详细信息。
There is a function for that! fflush($rc)
Did you make sure you had the correct resource as the parameter?
edit
Okay. looking at your code you are using socket_* functions. You need to use fsockopen, fwrite, fflush, fsockclose. Your code should work the same, but you will be able to flush output. check out the example in the php manual for more details.