473,785 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

socket timeout being ignored

Say I have the following script:

<?
$timeout = 5;
$start = time();
$fsock = fsockopen('125. 1.119.10',80,$e rrno,$errstr,$t imeout);

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

socket_set_time out($fsock,$tim eout);

fputs($fsock,"G ET http://www.google.com/ HTTP/1.0\r\n");
fputs($fsock,"H ost: 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 8421
yawnmoth wrote:
Say I have the following script:

<?
$timeout = 5;
$start = time();
$fsock = fsockopen('125. 1.119.10',80,$e rrno,$errstr,$t imeout);

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

socket_set_time out($fsock,$tim eout);

fputs($fsock,"G ET http://www.google.com/ HTTP/1.0\r\n");
fputs($fsock,"H ost: 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*******@attgl obal.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
environment s. 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 :)

>>Additionall y, 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*******@attgl obal.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,$e rrno,$errstr,$t imeout);

socket_set_time out($fsock,$tim eout);

fputs($fsock,"G ET http://www.google.com/ HTTP/1.0\r\n");
fputs($fsock,"H ost: 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*******@attgl obal.net
=============== ===
Aug 3 '06 #6

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

Similar topics

17
52224
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
4232
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 it seems to stay in the application scope, but the paranoid side of me thinks I may be missing something... any confirmation would be helpful.
3
4638
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 and then fires a datareceived event. Within the waitingloop there is a timeout function, but I want the the 'last-time-socket-used' variable set when the socket is finished sending. When I send by System.Net.Sockets.Socket.Send(buffer()) (<--this...
4
18135
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 as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call shutdown and close asynchronously if possible.
2
15334
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 now see the socket.RecieveTimeout 'property' in the visual studio 2005 help documentation (framework 2.0) and it has example of it being used with TCP socket. This propery is also listed as 'new in .net 2.0'. 1) is the socket.RecieveTimeout...
1
4457
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 nonblocking and uses "select()" to timeout as seen below in line 1487 and 1386 : static PyObject * 1470 sock_settimeout(PySocketSockObject *s, PyObject *arg) 1471 { 1472 double timeout;
2
2804
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 this error in fact seemed to come from the os (in this case windows xp). By inspection of test cases the error.reason from the deliberate socket timeout looks like 'timed out' whereas the windows caused timeout error.reason looks like
2
10737
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 socket, throw an exception. (3) Catch the exception and either go back to (1) or quit reading, depending on a variety of (user defined) factors.
2
3536
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 timeout.struct_to_timeout(self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeout.struct_size())) The helper timeout module is here:
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10327
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.