Jump to content

Sockets - Streaming to a Browser and Timing Out


markwod

Recommended Posts

Hi,

 

  I'm new to the PHP world. I've put together a simple script (below) with I intend to connect a server which sends out a stream of ASCII (I've a client in C++ with all works well). For now I just want it to read 250 bytes from the stream then somehow update a field on a brwoser.

 

Which has lead me to these two questions:

 

1) Whist debugging, the script does connect and read bytes ok. But when I stop debugging PHP is still keeping the connection live, so when the script runs again it fails to work. I've found a few time out methods, all of which see to have now effect.

 

2) Presumably using some javascript or other. can anyone suggest a resource which will give me an idea on how to this data back to the browser (as it comes in) and update the field without user interaction.

 

Many thanks

 

Mark

Code here sorry...:)

 

<?php

header("Cache-Control: no-cache, must-revalidate");

// Date in the past

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

 

$host="www.somehere.co.uk" ;

$port=8881;

$timeout=5;

 

$sk=fsockopen($host,$port,$errnum,$errstr,$timeout) ;

stream_set_timeout($sk, 2);

set_time_limit(10);

 

if (!is_resource($sk))

{

exit("connection fail: ".$errnum." ".$errstr) ;

}

else

{

    $dati="" ;

    while (!feof($sk))

    {

        $dati= fgets ($sk, 250);

        echo($dati) ;

    }

}

 

fclose($sk) ;

 

//output the response

echo($dati) ;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.