search
Homephp教程php手册用php程序检测window下端口是否运行

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)