3

I want my web server to notify me through a php page when an event occurs at another TCP server, to which the PHP page has successfully connected via a socket. The event is like the TCP server wants to send a message to the web server, etc. Is there any way to accomplish this and/or any references on how to do it?

oers
  • 18,436
  • 13
  • 66
  • 75
Izza
  • 2,389
  • 8
  • 38
  • 60

1 Answers1

2

Sure:

$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
        }
    }
}
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • Thanks for the reply, could you please explain what happens in this code please? – Izza Jun 14 '10 at 12:12
  • See the manual entries for fsockopen and fread (http://www.php.net/fsockopen and http://www.php.net/fread). I've fixed an error, `fsockopen` must be used in place of `fopen`. – Artefacto Jun 14 '10 at 12:26