简体   繁体   English

使用fsockopen()与服务器的连接保持打开状态; 在PHP中检查其状态

[英]Connections to my servers remaining open using fsockopen(); in PHP to check their status

I've been coding up a quick web app to check the status of all the different dedicated server processes I run on my multi-purpose server. 我一直在编写一个快速的Web应用程序,以检查在多功能服务器上运行的所有不同专用服务器进程的状态。 I'm using fsockopen to check the ports, and it's doing what I need it to do in that it's returning whether there is a service running on those ports, but it seems to be leaving the connections open. 我正在使用fsockopen检查端口,它正在做我需要做的事情,因为它返回这些端口上是否正在运行服务,但似乎使连接保持打开状态。 One specific instance is my Mumble server. 一个特定的实例是我的Mumble服务器。 Every time I run the script, it returns that my mumble server is online but the connection it opens with fsockopen is adding to the user count. 每次我运行脚本时,它都会返回我的拙劣服务器联机,但使用fsockopen打开的连接会增加用户数量。 It can keep going until it reaches the max user count and then will refuse connections from actual mumble clients as well as further attempts to check it with fsockopen . 它可以一直继续下去,直到达到最大用户数,然后再拒绝来自实际的拙劣客户端的连接,并进一步尝试使用fsockopen检查它。 The same happens when running my server for Terraria. 为Terraria运行服务器时,也会发生同样的情况。 Eventually, after probably at least an hour, the user count on my mumble server is resetting but I'm not certain if it is the connections closing or the server crashing and restarting. 最终,可能至少一个小时后,我指望的笨拙的服务器上的用户正在重置,但是我不确定是关闭连接还是服务器崩溃并重新启动。

Here's the code I'm using to check the ports and display on the page their status: 这是我用来检查端口并在页面上显示其状态的代码:

function serverPort($port, $service) {
    $fp = @fsockopen("192.168.1.xxx", $port, $errno, $errstr, 3);
        if($fp) {
            $color = "green";
            echo "<span style='font:12px/0px monospace'><b>$service</b></span>".
            spacer($service)."<span style='font:12px/0px monospace'> | 
            <b><font color=$color>online</font></span></b>\n";
            fclose($fp);
        } else {
            $color = "red";
            echo "<span style='font:12px/0px monospace'><b>$service</b></span>".
        spacer($service)."<span style='font:12px/0px monospace'> | 
        <b><font color=$color>offline</font></span></b>\n";
        fclose($fp);
        }
    stream_set_timeout($fp, 1);
    fclose($fp);
    $fp = null;

    echo "</br>";
}

I've got a function called in this code for dynamic spacing ( spacer(); ) on the output strings that is defined in the actual script prior to the function detailed here. 我在此代码中有一个函数,用于在实际脚本中定义的输出字符串上动态间隔( spacer(); ),此函数在此处详细介绍。 It's not relevent to the problem but I've left it in there to reduce confusion on my own part. 它与问题无关,但我将其留在这里是为了减少混乱。

As you can see near the end there I've tried three separate methods I've come across that I was under the impression were meant to close the connections but none of them have achieved the desired result. 如您将在末尾看到的那样,我已经尝试过碰到三种不同的方法,它们给我的印象是要关闭连接,但没有一种方法可以达到预期的效果。 I'm relatively new to PHP in that I haven't touched it since probably '04, same for HTML, so anything helps at this point. 我对PHP相对较新,因为自从'04以来就没有接触过它,对于HTML来说也一样,因此在这一点上有什么帮助。

Also, for reference, I've tried socket_create and socket_close together separately from fsockopen and it wouldn't even echo my intended results. 另外,作为参考,我已经尝试将socket_createsocket_closefsockopen分开尝试,它甚至都不会呼应我的预期结果。

Edit: And if anyone is curious, this is what it looks like: http://i.imgur.com/C9O3SMq.png 编辑:如果有人好奇,这就是它的样子: http : //i.imgur.com/C9O3SMq.png

It's completely functional, it's just the issue of the connections remaining open I need solved. 它完全功能正常,只是我需要解决的连接保持打开的问题。

found on php.net ( http://php.net/manual/en/function.fclose.php ) 在php.net上找到( http://php.net/manual/en/function.fclose.php

It is very important to make sure you clear any incoming packets out of the incoming buffer using fread() or some equivalent. 确保使用fread()或等效方法从传入缓冲区中清除所有传入数据包非常重要。 Although you can call fclose() the socket does not actually shut down until the inbound packets have been cleared. 尽管可以调用fclose(),但在清除入站数据包之前,套接字实际上不会关闭。 This can lead to some confusion. 这可能会导致一些混乱。

and

In case you have some trouble to properly disconnect some client streams opened with stream_socket_server / stream_select you should give a try to stream_socket_shutdown. 如果您在正确断开使用stream_socket_server / stream_select打开的某些客户端流时遇到麻烦,则应尝试使用stream_socket_shutdown。

<?php stream_socket_shutdown($clientStream,STREAM_SHUT_RDWR); <?php stream_socket_shutdown($ clientStream,STREAM_SHUT_RDWR); ?> ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM