php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50965 fsockopen will not work on 'localhost'
Submitted: 2010-02-08 11:06 UTC Modified: 2010-09-07 11:20 UTC
From: tony at marston-home dot demon dot co dot uk Assigned:
Status: Not a bug Package: Sockets related
PHP Version: 5.2.12 OS: Windows XP
Private report: No CVE-ID:
 [2010-02-08 11:06 UTC] tony at marston-home dot demon dot co dot uk
Description:
------------
When are you going to provide a proper answer for post http://bugs.php.net/bug.php?id=50953 ? I have removed the IPv6 entries in my hosts file, but the problem is still there.

Reproduce code:
---------------
function connect($host) {
    $faultcode   = null;
    $faultstring = null;
    $conn = fsockopen($host, 80, $faultcode, $faultstring, 20);
    if (!$conn) {
    	echo 'faultcode=' .$faultcode .', faultstring=' .$faultstring
."\n";
    } else {
        echo "Connected to $host OK\n";
    } // if
    return $conn;
} // function

$result = connect('localhost');
$result = connect('127.0.0.1');
$result = connect('desktop');
$result = connect('www.tonymarston.net');



Expected result:
----------------
I expect to see the message "Connected to <host> OK" for all values of host.


Actual result:
--------------
Warning:  fsockopen(): unable to connect to localhost:80
faultcode=10060, faultstring=A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to
respond.

Connected to 127.0.0.1 OK
Connected to desktop OK
Connected to www.tonymarston.net OK


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-08 11:16 UTC] aharvey@php.net
Please don't open duplicate bugs in an attempt to "bump" existing open bugs. It's been less than 19 hours since your last post in bug #50953; I'm quite sure Pierre will respond when he's able.

Closing.
 [2010-02-08 11:18 UTC] aharvey@php.net
Correction: bug #50953 isn't open. That still doesn't merit opening a new one, IMO.
 [2010-02-08 11:35 UTC] tony at marston-home dot demon dot co dot uk
That is because he closed the  bug without waiting to see if his suggestion about removing the IV6 entries from the hosts file actually worked.

He he hasn't provided a solution then the bug is not closed.
 [2010-02-08 11:40 UTC] aharvey@php.net
OK, bug #50953 has been reopened. In future, please post in the original bug.
 [2010-02-08 11:57 UTC] tony at marston-home dot demon dot co dot uk
I did respond in the original bug, but you closed it before finding out if your suggestion about removing the IPV6 entries from my hosts file actually worked. I put in a reply saying that it hadn't worked, but you never re-opened the bug. I assumed that as you had closed it that you were refusing to answer any more queries about it.
 [2010-02-08 17:07 UTC] rasmus@php.net
If 127.0.0.1 works and your name lookup system correctly returns 
127.0.0.1 for localhost, then localhost has to work.  If it isn't 
working it is because the lookup isn't returning the correct ipv4 ip.  
You may have removed it from your hosts file, but perhaps the old 
answer is cached somewhere.  Try restarting things after changing your 
hosts file.
 [2010-02-08 17:32 UTC] tony at marston-home dot demon dot co dot uk
I have checked my hosts file, and it only contains the following entry for 'localhost':

127.0.0.1       localhost

I have rebooted my PC to clear any cache, but the error is still there.

The name 'localhost' works in all the browsers that I use - IE, Firefox, Opera and Safari - so it should work with fsockopen. It works with names I use for other PCs in my home network.

I have another PC running Windows XP with IPV6 enabled and PHP 5.3.0, and fsockopen doesn't have a problem using 'localhost' there.
 [2010-02-08 17:39 UTC] rasmus@php.net
There is no special-case code in PHP for localhost.  
Try: print_r(gethostbynamel('localhost'));

 [2010-02-08 17:55 UTC] tony at marston-home dot demon dot co dot uk
print_r(gethostbynamel('localhost'));  produces the following:

Array
(
    [0] => 127.0.0.1
)
 [2010-09-07 11:20 UTC] pajoye@php.net
Fix and discussions will go now into the bug #50953.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 13:02:46 2014 UTC