相关文章推荐
奔放的四季豆  ·  Epplus c# to excel 的 ...·  1 年前    · 
逃跑的键盘  ·  windows - CMD command ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

$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"; 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.

$fp = fsockopen ("localhost", 80, $errno, $errstr, 10); if (!$fp) { echo "$errstr ($errno) }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.

Connection timed out usually points to a firewall or an answer from the remote side that just takes too long. Gerald Schneider Aug 14, 2014 at 10:17 Are you sure your firewall correctly forwarded tcp port 7550 on the target server? Because usually a webserver is running in port 80 which is open ofcourse opened if you can enter websites. Steini Aug 14, 2014 at 10:20 @GeraldSchneider I know whats the meaning of Connection timed out but I open http://XX.XX.XX.XX:7550 on proxy then it is taking only 1 sec to open in browser. Muhammad Hassan Aug 14, 2014 at 10:29 If you want network communication with advanced functionality, use socket_create instead of fsockopen . Daniel W. Aug 14, 2014 at 10:30

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.

No. If you need check if a port is online in a IP (and not in a domain) you need handle the exception or use the @. If you handle exception then you will need the if condition and try, catch. Read php.net/manual/es/function.fsockopen.php. Show errors in a production environment is bad. – SnakeDrak Aug 14, 2014 at 10:22 @Snake I am able to open http://XX.XX.XX.XX:7550 in my Web-Browser using proxy and also your link is showing that my 7550 port is open. – Muhammad Hassan Aug 14, 2014 at 10:26 @Snake When I added $fp = @fsockopen($site,$port,$errno,$errstr,10); then I got 110 : Connection timed out SERVER IS DOWN... :( – Muhammad Hassan Aug 14, 2014 at 10:27 Can you open a socket to other IP or domain? The first is identify the problem. (Obviously with fsockopen) Have you tested with socket_create? – SnakeDrak Aug 14, 2014 at 10:37

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
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.