PHP判斷伺服器連接埠是否開啟的代碼

來源:互聯網
上載者:User

記錄一下,以後備用

 
  1. <?php 
  2. $host = 'www.google.com'; //要ping的地址,也可以是IP  
  3. $port = '80'; //要ping的連接埠  
  4. $num = 3;  
  5. function microtime_float()  
  6. {  
  7. list($usec, $sec) = explode(" ", microtime());  
  8. return ((float)$usec + (float)$sec);  
  9. }  
  10. function ping($host,$port)  
  11. {  
  12. $time_start = microtime_float();  
  13. $ip = gethostbyname($host);  
  14. $fp = @fsockopen($host,$port,&$errno,&$errstr,1);  
  15. if(!$fp) return 'replay time out!';  
  16. $get = "GET / HTTP/1.1\r\nHost:".$host."\r\nConnection: Close\r\n\r\n";  
  17. @fputs($fp,$get);  
  18. @fclose($fp);  
  19. $time_end = microtime_float();  
  20. $time = $time_end - $time_start;  
  21. $time = ceil($time * 1000);  
  22. return 'Reply from '.$ip.': time='.$time.'ms<br />';  
  23. }  
  24. echo 'Pinging '.$host.' ['.gethostbyname($host).'] with Port:'.$port.' of data:<br /><br />'."\r\n";  
  25. for($i = 0;$i < $num;$i++)  
  26. {  
  27. ping($host,$port);  
  28. sleep(1);  
  29. ob_flush();  
  30. flush();  
  31. }  
  32. ?> 

本文摘自 http://www.cnblogs.com/luoine/archive/2010/12/01/1893156.html




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.