Re: re:Timing out an fgets
| From: | David Robley | Date: | Fri, 18 Aug 2000 02:00:53 +0000 |
| Subject: | Re: re:Timing out an fgets | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-12391@lists.php.net to get a copy of this message | ||
On 17 Aug, Joseph H Blythe wrote:
> Joseph H Blythe wrote:
>
>> Hey php'ers
>>
>> I am currently developing a client for a server application, it is near
>> completion but I still have to cover a few error conditions one being no
>> response from the server at all.
>>
>> The application im using has provision to test this situation and allows
>> you to send packets to it but will not respond at all.
>>
>> My problem is I am using fgets to receive a result from the server, but
>> every time I test for this condition it just sits there trying and
>> eventually ( after about 5 mins ) says page cannot be displayed.
>>
>> My question is how would I go about setting a time-out for the fgets
>> command so that I may retry a number of times before sending a error
>> message.
>>
>> I have tried something like this, of course I have already opened a
>> socket and $pck would hold the info im trying to send.
>>
>> ...
>>
>> $i = 0;
>> while( !fgets($fp, 1024) ) {
>> if ( $i <= 2 ) {
>> sleep(10);
>> fputs($fp, $pck);
>> or die("Error connecting to remote host");
>> } else {
>> die("Maximum retry count reached");
>> }
>> if ( fgets($fp, 1024) ) {
>> break;
>> } else {
>> $i++;
>> continue;
>> }
>> }
>>
>> ...
>>
>> The above code would handle the retries, but not a timeout as fgets just
>> seems to keep trying until the browser timesout the page?
>>
>
> I may have found a possible soultion in php's connection handling
> functions, but I can't seem to get them to work!
>
> set_connection_timeout(10);
>
> while ( connection_timeout() == 0 ) {
> fgets($fp,1024);
> or die("No response");
> if ( connection_timeout() == 1 ) {
> die("Connection Timed Out");
> }
> }
>
> Now this don't work either my script refuses to timeout even with the
> default timeout of 30 seconds, also I am unsure of what value connection
> timeout is returning is it 0 for false and 1 for true or TRUE/FALSE????
>
> Anyways there must be someway to tell the script to timeout, if I can get
> this to work I can also run a function when the user aborts using the stop
> button which will come in handy with connection_aborted.
>
> Please help!
>
> Regards,
>
> Joseph Blythe
The manual says that connection_timeout returns true if the script has
timed out. But in your example above, shouldn't you be setting
max_execution_time to reset the timeout? I can't see a
set_connection_timeout anywhere in the docs?
Also, if you are using fsockopen, is the timeout argument not
functional?
Some theoretical thoughts from someone who has never used this stuff :-)
Cheers
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA