Heim  >  Artikel  >  php教程  >  用php程序检测window下端口是否运行

用php程序检测window下端口是否运行

WBOY
WBOYOriginal
2016-06-06 19:49:011214Durchsuche

本篇文章主要分享一下如何使用php程序检测window平台系统中的端口是否正在运行,比如我们下面检测mysql端口3306,或者检测openoffice端口8100是否正在运行。 方法一:《推荐》 $server = "127.0.0.1";//检测的服务地址,如果是本地及用127.0.0.1或者localhos

本篇文章主要分享一下如何使用php程序检测window平台系统中的端口是否正在运行,比如我们下面检测mysql端口3306,或者检测openoffice端口8100是否正在运行。

方法一:《推荐》
$server = "127.0.0.1";//检测的服务地址,如果是本地及用127.0.0.1或者localhost即可,如果其他window服务地址则用其ip地址即可。
$port = "8100";//我们要检测制定端口号
$timeout = "2";//这个地方是超时时间,即2秒钟,可以随意设定
if($server and $port and $timeout){
   $verbinding = @fsockopen("$server", $port, $errno, $errstr, $timeout);
}
if($verbinding) {
   echo "The port is online";
}else {
   echo "The port  is offline";
}
方法二:

function test_port($host,$port,$timeout=2){//检测公共函数($host:检测服务地址;$port:要检测的端口号;$timeout:超时时间,默认两秒钟)
  $fsock = @fsockopen($host, $port, $errno, $errstr, $timeout);
  if (!$fsock){
     return FALSE;
  }else{
     return TRUE;
  }
}

/* 我们调用上面的检测函数,检测本地的8100端口服务是否已经启动*/
echo $ok = test_port('127.0.0.1',8100,3);

以上是用来检测window平台端口的相关方法。
总结:针对以上方法如果是经常用在系统中并且需要不间断的执行检测的话,我推荐使用第一种方法,因为第一种方法不会出现阻塞的情况,基本上第一次检测时可能花费时间比较长一点,但是一旦达到设定的超时值会自动返回相关检查结果,如果是那种不间断的检测服务的话,除了第一次,以后的每一次检测操作都会很快的处理完成。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:实例化php类的时候如何传参Nächster Artikel:别迷糊了!PHP