作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在端口 8195 上创建一个简单的监听器。当我在 PHP CLI 条件下尝试以下代码块时,它只显示一次“测试”,然后挂起。如果我删除文件“votifier.run”,该文件设计为开/关开关,它仍然会继续挂起。它从不显示“客户端已连接”。
此外,如果我在脚本运行时尝试通过端口 8195 上的 Telnet 连接到主机,我只会收到一条连接失败消息。就像它正在寻找一种联系,只是不放弃。
// Set the IP and port to listen to
$address = 'localhost';
$port = 8195;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port);
// Start listening for connections
socket_listen($sock);
// Loop continuously
while ( file_exists('votifier.run') ) {
echo 'Test';
$client = socket_accept($sock);
if( $client ) {
echo 'Client connected';
// Don't hang on slow connections
socket_set_timeout($client, 5);
// Send them our version
socket_write("VOTIFIER MCWEBLINK\n");
// Read the 256 byte block
$block = socket_read($client, 256);
...
最佳答案
我只是在这里猜测,但是您尝试绑定(bind)的地址可能不是主机名吗?
If the socket is of the AF_INET family, the address is an IP in dotted-quad notation (e.g. 127.0.0.1).
if( $client ) {
echo 'Client connected';
// Don't hang on slow connections
socket_set_option(
$client,
SOL_SOCKET,
SO_RCVTIMEO | SO_SNDTIMEO,
array('sec' => 5, 'usec' => 0)
);
// Send them our version
socket_write($client, "VOTIFIER MCWEBLINK\n");
^^^^^^^
// Read the 256 byte block
$block = socket_read($client, 256);
关于php - socket_accept 似乎卡在第一个连接上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11948268/
我正在尝试在端口 8195 上创建一个简单的监听器。当我在 PHP CLI 条件下尝试以下代码块时,它只显示一次“测试”,然后挂起。如果我删除文件“votifier.run”,该文件设计为开/关开关,
你好,我做了一个 php 套接字服务器来从 plc 获取数据,plc 被配置为 tcp 套接字客户端。 我有一个严重的问题,如果本地网络出现故障,似乎功能 socket_accept 停止,plc 无
我是一名优秀的程序员,十分优秀!