473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

socket timeout being ignored

Say I have the following script:

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

// 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?

Aug 2 '06 #1
5 8394
yawnmoth wrote:
Say I have the following script:

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

// 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.
js*******@attglobal.net
==================
Aug 2 '06 #2

Jerry Stuckle wrote:
yawnmoth wrote:
<snip>

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?
That's a good point - it's been fixed :)
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.
So I guess I should just use fread with as big a number as possible?
Like pow(2,20) or something?

The fact that fgets (and fread), though, can return empty lines, even
when they're in blocking mode, makes me think that this wouldn't work?
(I made another post about this)

Aug 2 '06 #3
yawnmoth wrote:
Jerry Stuckle wrote:
>>yawnmoth wrote:
<snip>

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?

That's a good point - it's been fixed :)

>>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.

So I guess I should just use fread with as big a number as possible?
Like pow(2,20) or something?

The fact that fgets (and fread), though, can return empty lines, even
when they're in blocking mode, makes me think that this wouldn't work?
(I made another post about this)
Could be. I haven't seen that occur, but there could be a bug in it.
You could try a big fread() call. Don't know what it will do. I
haven't used the timeout parm like that. Normally I want to get the
results even if it takes a few second.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 3 '06 #4

Jerry Stuckle wrote:
yawnmoth wrote:
<snip>
Could be. I haven't seen that occur, but there could be a bug in it.
You could try a big fread() call. Don't know what it will do. I
haven't used the timeout parm like that. Normally I want to get the
results even if it takes a few second.
I actually would like to get the results, too - just not at the cost of
a few minutes, heh.

Aug 3 '06 #5
yawnmoth wrote:
Jerry Stuckle wrote:
>>yawnmoth wrote:
<snip>
Could be. I haven't seen that occur, but there could be a bug in it.
You could try a big fread() call. Don't know what it will do. I
haven't used the timeout parm like that. Normally I want to get the
results even if it takes a few second.

I actually would like to get the results, too - just not at the cost of
a few minutes, heh.
In that case I would suggest trying to track down the source of the problem.

If the socket opens OK, you have a connection to the server (what is
this server, anyway - I don't get a response at that IP). It shouldn't
take Google too long to respond since you're not searching. In this
case an IP trace might help.

Another alternative, BTW, to stop processing, would be to check the time
in your loop rather than wait for the timeout. If processing exceeds 5
seconds, stop. For instance:

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

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) && time() < ($start + $timeout))
{
echo fgets($fsock);
}

fclose($fsock);

This should terminate loop processing as soon as it returns from the
fgets() and you've exceeded you 5 second timeout.

It might allow you to see what you're getting and help track down the
problem.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Achim Domma | last post by:
Hi, I'm using Python 2.3s timeout sockets and have code like this to read a page from web: request = ... self.page = urllib2.urlopen(request) and later:
5
by: Russell Warren | last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it a cross-process/system setting or does it stay local in the application in which it is called? I've been testing this and...
3
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, I use the System.Net.Sockets to send/receive data (no tcpclient/tcplistener), I made a receivethread in my wrapper, the receivethread loops/sleeps while waiting for data...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
1
by: jnair | last post by:
When using socket.socket.settimeout we normally only guard against "socket.timeout" exception.Now the implementation of "settimeout" in "Python-2.4.3/Modules/socketmodule.c" sets the socket fd to...
2
by: Robin Becker | last post by:
While messing about with some deliberate socket timeout code I got an unexpected timeout after 20 seconds when my code was doing socket.setdefaulttimeout(120). Closer inspection revealed that...
2
by: carl.rosenberger | last post by:
Hi, I am trying to get the following behaviour for my Socket connection: (1) Attempt a blocked read for a defined amount of time. (2) If a timeout occurs, because no data has been sent to the...
2
by: Heikki Toivonen | last post by:
M2Crypto has some old code that gets and sets socket timeouts in http://svn.osafoundation.org/m2crypto/trunk/M2Crypto/SSL/Connection.py, for example: def get_socket_read_timeout(self): return...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.