2010-06-14 211 views
3

我要在事件发生在另一个TCP服务器,PHP页面已经通过一个套接字连接成功我Web服务器通过一个PHP页面通知我。本次活动是像TCP服务器希望将消息发送到Web服务器,等有没有办法做到这一点和/或如何做到这一点的任何引用?事件监听器在PHP

回答

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 
     } 
    } 
} 
+0

感谢您的答复,请您解释一下这个代码会发生什么吗? – Izza 2010-06-14 12:12:50

+0

参见手册条目的fsockopen和的fread(http://www.php.net/fsockopen和http://www.php.net/fread)。我已经修复了错误,'fsockopen'必须到位fopen'的'使用。 – Artefacto 2010-06-14 12:26:58