Re: re:Timing out an fgets

From: Date: Thu, 17 Aug 2000 01:58:59 +0000
Subject: Re: re:Timing out an fgets
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-12139@lists.php.net to get a copy of this message
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

« previous php.general (#12139) next »