传输端点未连接
我的代码是这样的,但不起作用
<?php
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
$sk = socket_connect($sock,"10.0.1.43","1234");
socket_set_nonblock($sock);
while (1) {
sleep(2);
$buffer=socket_read($sock,512);
echo "Buffer = $buffer \n";
echo "Last Error = ".socket_last_error($sock).socket_strerror(socket_last_error($sock))."\n";
}
?>
它显示错误:
无法从套接字读取[107]:传输端点未连接 PHP 堆栈跟踪: 缓冲区=
最后一个错误= 107传输端点未连接
谢谢
My code is this but not working
<?php
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
$sk = socket_connect($sock,"10.0.1.43","1234");
socket_set_nonblock($sock);
while (1) {
sleep(2);
$buffer=socket_read($sock,512);
echo "Buffer = $buffer \n";
echo "Last Error = ".socket_last_error($sock).socket_strerror(socket_last_error($sock))."\n";
}
?>
It display error:
unable to read from socket [107]: Transport endpoint is not connected
PHP Stack trace:
Buffer =
Last Error = 107Transport endpoint is not connected
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须首先接受连接!在您的
while()
循环中,执行另一个while()
操作,如下所示:它应该可以正常工作,如您所愿。
You have to accept a connection first! Inside your
while()
loop, do anotherwhile()
like this:It should work, as you intend.
好的,所以客户端套接字没有连接。
什么是协议“0”?您确定“0”是您系统上的 TCP 吗?
不确定是否可以在 connect() 之后更改套接字阻塞/非阻塞状态 - 从未尝试过这样的事情。
如果 $sk 为 false,最后一个错误是什么?
是否可以通过 TCP 访问位于 10.0.1.43:1234 的服务器?
平均值,
马丁
OK, so the client socket is not connected.
What is protocol '0'? Are you sure that '0' is TCP on your system?
Not sure if you can change the socket block/non-block state after a connect() - never tried such a thing.
If $sk is false, what is the last error?
Is the server at 10.0.1.43:1234 reachable with TCP?
Rgds,
Martin
正如我在对上一个答案的评论中所写的那样,您可以在 PHP 上找到解决此问题的很好的示例.net 示例页面。在主文章中,您将找到一个很好的示例,如何为一个客户端创建一个没有错误的侦听器,并且在下面的哈维尔通知中,如何为多客户端目的创建相同的侦听器。
As I wrote in a comment to previous answer, you can find fine example of solving this problem on PHP.net Examples Page. In the main article you'll find a good example, how to make a listener without error, you're refering to, for one client and in javier's notice below, how to have the same for multi-client purpose.