search
Homephp教程PHP源码PHP判断端口是否打开的代码

跳至

<?php
 2    $host = &#39;www.baidu.com&#39;; //要ping的地址,也可以是IP
 3     $port = &#39;80&#39;; //要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 &#39;replay time out!&#39;;
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 &#39;Reply from &#39;.$ip.&#39;: time=&#39;.$time.&#39;ms
&#39;;
23  }
24  echo &#39;Pinging &#39;.$host.&#39; [&#39;.gethostbyname($host).&#39;] with Port:&#39;.$port.&#39; of data:

&#39;."\r\n";
25  for($i = 0;$i < $num;$i++)
26  {        
27          ping($host,$port);
28          sleep(1);
29          ob_flush();
30          flush();
31  }
32  ?>

                   

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.