如何在PHP中实现这样的功能呢?

发布于 2024-08-19 04:33:48 字数 61 浏览 3 评论 0原文

它应该测试本地主机上是否打开特定端口,如果没有,则重新启动。

它在 Windows 中运行。

It should test if a specific port is open on localhost,if not,reboot.

It's run in windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万劫不复 2024-08-26 04:33:48

这应该可以做到,在 Windows 7 上测试并工作。应该适用于所有 NT 口味:

function testPort($port, $timeout = 5) {
    if(!fsockopen('127.0.0.1', $port, $errno, $errstr, $timeout)) {
        exec("shutdown.exe /r");
    }
}

testPort(8080);

This should do it, tested on Windows 7 and working. Should work on all NT flavours:

function testPort($port, $timeout = 5) {
    if(!fsockopen('127.0.0.1', $port, $errno, $errstr, $timeout)) {
        exec("shutdown.exe /r");
    }
}

testPort(8080);
绅士风度i 2024-08-26 04:33:48

您可以编写一个 PHP 扩展来做到这一点。扩展应该使用 Windows API 来重新启动机器,因为套接字检查部分可以直接在 PHP 中完成。 这里是一个关于如何编写扩展的问题。

InitiateSystemShutdown 是您可以调用的 Win32 API 函数进行实际的重新启动。

You can write a PHP extension to do that. Extension should use Windows API to reboot the machine because the socket checking part can be done straight in PHP. Here is a question on how to write extensions.

InitiateSystemShutdown is the Win32 API function you can call to do the actual rebooting.

你好,陌生人 2024-08-26 04:33:48

使用插座
您需要使用该特定端口打开到本地主机的 TCP 连接(套接字)。如果连接建立,则表示端口打开,否则(如果超时或拒绝),则端口关闭。

仅适用于 TCP 端口

这里有一个示例代码

对于“重新启动”部分,使用 exec('shutdown -r');

Use sockets
you need to open a TCP connection (socket) to the localhost with that specific port. If the connection is establish, it means the port is open, otherwise (if timed-out or rejected), then the port is closed.

that's only for TCP ports

here's a sample code

For the 'reboot' part, use exec('shutdown -r');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文