How To Solve [Warning: fsockopen(): unable to connect]?

余生颓废 提交于 2019-12-06 06:54:27

问题


I want to check that my server Ubuntu Server 14.04 LTS is online or not at my shared hosting server. My server has IP not domain but my shared hosting server has domain. For this purpose I used the below code in my shared hosting server to check that my server is online or not.

<?php
$site = "XX.XX.XX.XX";
$port = 7550;
$fp = fsockopen($site,$port,$errno,$errstr,10);
if ($fp === false) { 
 print($errno." : ".$errstr); 
}  
if(!$fp)
{
echo "SERVER IS DOWN";
}
else
{
echo "SERVER IS UP ON PORT ".$port." AT ".$site;
fclose($fp);
}
?>

After adding the above code in my shared hosting server php file, when I run it then I got the below error.

Warning: fsockopen(): unable to connect to XX.XX.XX.XX:7550 (Connection timed out) in /home/USERNAME/public_html/index.php on line 4 110 : Connection timed out SERVER IS DOWN

Now somebody tell me to check that allow_url_fopen = On is turn on in my shared hosting server or not then I checked my shared hosting server php.ini file and there it is turn on.

Now as I was looking for help all around then somebody else told me to check both(my shared hosting server and my server) have fsockopen() ON or not. Then I wrote the below code in a php file and run on both servers.

<?php
$fp = fsockopen ("localhost", 80, $errno, $errstr, 10);
if (!$fp) {
echo "$errstr ($errno)
\n";
}else
{
echo "fsockopen Is Working Perfectly.";
}
fclose ($fp);
?>

After running the above .php file on both servers then I got the below result same on both server.

fsockopen Is Working Perfectly.

Important Note: Keep in mind that my server IP remain active as I am using many PC on that IP but my server is turn off. Also I am able to open http://XX.XX.XX.XX:7550 in my Web-Browser using proxy.


回答1:


Because fsockopen throws a E_WARNING exception if hostname is not a valid domain. So, you use a IP with a PORT offline then the function throws the exception.

You can solve with handle the exception or @:

$fp = @fsockopen($site,$port,$errno,$errstr,10);

If you think that you PORT is online, check the firewall and after check with a external resouce like http://www.yougetsignal.com/tools/open-ports/.

Read http://php.net/manual/es/function.fsockopen.php.

UPDATED: You could test with socket_create, it's better that fosockopen.

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection =  @socket_connect($socket, 'XX.XX.XX.XX', 7550);

if( $connection ){
    echo 'ONLINE';
}
else {
    echo 'OFFLINE: ' . socket_strerror(socket_last_error( $socket ));
}

You must set a valid protocol, see: http://php.net/manual/es/function.socket-create.php.




回答2:


If you overwrote the error handler with set set_error_handler() you need to check if error_reporting() returns 0 which means an @ was used to suppress errors.

set_error_handler('errorHandler');

function errorHandler($errno, $errstr, $errfile, $errline) {

    if (error_reporting() === 0) 
        return false;

    //handle error
    ...
}


来源:https://stackoverflow.com/questions/25305417/how-to-solve-warning-fsockopen-unable-to-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!