socket_create 与 fsockopen php
我当前使用的托管服务不允许我使用套接字,这可能是他们有充分的理由。 不过,他们确实让我使用fsockopen。 我想知道有什么区别,因为一些适用于 socket_create 甚至 stream_socket_server 的脚本,不适用于 fsockopen。 也就是说,如果 fsockopen 应该工作,我的代码如下所示。 它的作用是在自己的 IP 地址上侦听传入的 udp 数据包并读取它们。
谢谢
$sock = fsockopen("udp://x.x.x.x", $port);
while(1)
{
$buf = fread($sock, 200);
flush();
ob_flush();
}
The hosting service that I use currently does not let me use sockets, probably for good reason on their part. They do, however, let me use fsockopen. I was wondering what the difference is, because some scripts that worked with socket_create and even stream_socket_server, do not work with fsockopen. That said, if fsockopen should work, my code is listed below. What it does is it listens on its own ip address for incoming udp packets and reads them.
Thanks
$sock = fsockopen("udp://x.x.x.x", $port);
while(1)
{
$buf = fread($sock, 200);
flush();
ob_flush();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
fsockopen 创建到主机的连接,而不是侦听套接字。
fsockopen($address) ~== socket_connect(socket_create(), $address)
您的托管提供商不希望您监听在备用端口/协议上。
如果你所拥有的有效,我不会指望它总是有效,因为这将是一个错误。
fsockopen creates a connection to a host, not a listening socket.
fsockopen($address) ~== socket_connect(socket_create(), $address)
Your hosting provider doesn't want you listening on alternate ports/protocols.
If what you have works, I wouldn't count on it always working as it would be a bug.