3

当另一个 TCP 服务器上发生事件时,我希望我的Web 服务器通过 php 页面通知我,而 PHP 页面已通过套接字成功连接到该服务器。该事件就像 TCP 服务器想要向 Web 服务器发送消息等。有没有办法完成这个和/或关于如何做到这一点的任何参考?

4

1 回答 1

2

当然:

$fp = fsockopen("tcp://example.com", 8888) OR die("could not connect");
while (!feof($fp)) {
    $pc = fread($handle, 8192);
    if ($pc === false || strlen($pc) == 0)
        break;
    //a new packet has arrived
    //you should collect the read in a variable and wait
    //for another packet until you know the message is complete
    //(depends on the protocol)
    collect_in_result($pc);
    if (message_is_complete()) {
        if (check_event()) {
            //take action
        }
    }
}
于 2010-06-14T10:11:53.947 回答