开发者

How to implement such a function in PHP?

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

开发者_如何学Python

It's run in windows.


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);


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.


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');

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜