|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40427 fsockopen() always forces a DNS resolution unnecessarily
Submitted: 2007-02-10 02:33 UTC Modified: 2007-02-10 19:51 UTC
From: andrew at neonsurge dot com Assigned:
Status: Not a bug Package: Streams related
PHP Version: 5.2.1 OS: Gentoo, OS-X 10.4
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
47 + 1 = ?
Subscribe to this entry?

 [2007-02-10 02:33 UTC] andrew at neonsurge dot com
fsockopen() always asks your DNS server for the IP address 
of a connection before it opens it, instead of trying to 
look in the local cache first.  I believe this is unexpected 
and performance-wise is a very undesirable operation.  See 
reproduce code...

Reproduce code:
<?php for ($i = 0; $i < 50; $i++) {
	$errno = $errstr = "";
	//$ip = gethostbyname("");  $a = fsockopen($ip,22,$errno,$errstr,10);  //FAST way
	$a = fsockopen("",22,$errno,$errstr,10); //SLOW way
	$ab = fread($a,4096); unset($a, $ab);
} ?>

Expected result:
While running the slower code above exactly as 
is, you can packet sniff and view 50 full requests for the 
domain  This is unoptimized and causes this code to 
run extremely slow.

When running with the fast way (change lines commented out) 
the above code runs a few hundred percent faster.  Upon a 
packet sniff you can watch 1 or two requests for 
being sent to your DNS server which I believe is the desired 

NOTE: In the code above I do have the workaround by 
connecting with the IP instead by using gethostbyname() 
first which appears to use the local DNS cache.

Actual result:
The code above should run exactly the same speed either way.  
fsockopen should be using the local machine's DNS cache and 
not have to query the DNS server upon every request when 
given a DNS name to connect to.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-10 19:51 UTC]
>fsockopen() always asks your DNS server for the IP address 
>of a connection before it opens it, instead of trying to 
>look in the local cache first. 

Believe me, fsockopen() knows nothing about DNS servers and local caches.
Not PHP problem.
 [2011-10-11 06:26 UTC] georg dot hoellrigl at xidras dot com
Just for completness: The problem still existis in newer PHP Versions even if it may come from the used system calls.

There is a simple solution to this: Disable IPv6 and try again...
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jul 19 07:01:30 2024 UTC