Re: socket timeout being ignored



yawnmoth wrote:
Say I have the following script:

<?
$timeout = 5;
$start = time();
$fsock = fsockopen('125.1.119.10',80,$errno,$errstr,$timeout);

// reduce $timeout by the amount of time that it took for fsockopen to
connect.
$timeout-=(time()-$start);

socket_set_timeout($fsock,$timeout);

fputs($fsock,"GET http://www.google.com/ HTTP/1.0\r\n");
fputs($fsock,"Host: www.google.com\r\n\r\n");

while ( !feof($fsock) )
{
echo fgets($fsock);
}

fclose($fsock);
?>

As you can tell, the timeout is set to 5 seconds in fsockopen and then
is set to how ever many seconds out of that 5 that fsockopen didn't
take to connect.

All in all, the whole script should run in, at most, 5 seconds. Or
atleast it seems like it should. Sometimes, however, it doesn't - it
takes several minutes. And unfortunately, this happens enough times to
be kinda a nuisance. And I have no idea what the problem is.

Any ideas?


What happens if the fsockopen() takes longer than 5 seconds?

See the note on fsockopen() - the timeout may not be available in all environments. So a delay due to routing, server load or whatever may cause the open to take longer than 5 seconds, putting a negative value in your $timeout. I don't know what PHP will do with a negative value - maybe convert it to a very large positive value?

Additionally, the timeout restarts every time you go a fgets(). The server could be sending one line every 4 seconds, for instance. 10 lines would be 40 seconds with no errors.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: socket timeout being ignored
    ... See the note on fsockopen- the timeout may not be available in all ... So a delay due to routing, server load or whatever may ... So I guess I should just use fread with as big a number as possible? ...
    (comp.lang.php)
  • Re: socket timeout being ignored
    ... See the note on fsockopen- the timeout may not be available in all ... So a delay due to routing, server load or whatever may ... You could try a big fread() call. ...
    (comp.lang.php)
  • Re: Socket recv() question?
    ... There's a limited amount of RAM in CE devices. ... Configure tx timeout ... increasing the timeout using the select method on the socket descriptor. ... sockets to send binary data from a pc to Pocket PC. ...
    (microsoft.public.windowsce.app.development)
  • mod_perl 1: Apache::print() and timeouts
    ... list and the mod_perl Development mailing list, but with no response. ... if I look at the documentation for the "Timeout" directive ... The total amount of time it takes to receive a GET request. ... The amount of time between ACKs on transmissions of TCP packets ...
    (comp.lang.perl.misc)
  • Re: Always On Top/Not Movable/Not Closeable, then not
    ... Now I need to keep it on top and not allow it to be moved or closed, but after a certain amount of time, allow it to be moved until the full timeout where it would automatically close. ... How can I allow the window to be moved without showing the close window button? ... Petr Vileta, Czech republic ...
    (comp.lang.perl.tk)