gpt4 book ai didi

php - socket_accept 似乎卡在第一个连接上

转载 作者:行者123 更新时间:2023-12-03 11:54:22 33 4
gpt4 key购买 nike

我正在尝试在端口 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);
...

答案:
socket_accept() 通常会挂起,直到建立连接。如果进行了连接尝试,脚本将继续,但因为套接字是在 localhost 上创建的,所以它只接受来自 localhost 的连接。

解决方法是使用您的外部 IP 而不是“localhost”或“127.0.0.1”。然后你可以 Telnet 到它。

最佳答案

我只是在这里猜测,但是您尝试绑定(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).



编辑

好的,我已使用您的脚本并尝试重现您的错误,但无法重现。它有几个缺陷,但没有一个会导致 telnet 客户端的连接尝试失败。

由于上述都不适用,让我们一一检查 list :
  • 已加载/编译的套接字模块
  • localhost 确实解析为 127.0.0.1
  • 该端口未被任何其他运行
  • 的应用程序占用
  • 没有任何防火墙规则会阻止 telnet 客户端和您的服务器之间的通信
  • 允许您连接的机器连接到服务器主机(如果不是,请尝试相同的主机)
  • 在 while 循环中检查的文件确实存在
  • 您确定您的脚本中没有另一个 fatal error 会阻止您发布的代码段运行

  • 这些都是我能想到的所有可能的错误来源,atm。首先尝试修复小缺陷,然后检查 list 。
    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/

    33 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com