Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 644
Muhammad Hassan Avatar asked Feb 13 '23 06:02

Muhammad Hassan


2 Answers

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.

like image 175
SnakeDrak Avatar answered Feb 14 '23 19:02

SnakeDrak


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
    ...
}
like image 25
Torge Avatar answered Feb 14 '23 20:02

Torge