Home > Article > Backend Development > How to implement TCP port detection in php_PHP tutorial
This article describes the method of implementing TCP port detection in php. Share it with everyone for your reference. The details are as follows:
This program can confirm whether the current port is available:
4 11 12 |
<๐>class Health {<๐> <๐>public static $status;<๐> <๐>public function __construct()<๐> <๐>{<๐> <๐>}<๐> <๐>public function check($ip, $port){<๐> <๐>$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);<๐> <๐>socket_set_nonblock($sock);<๐> <๐>socket_connect($sock,$ip, $port);<๐> <๐>socket_set_block($sock);<๐> <๐>self::$status = socket_select($r = array($sock), $w = array($sock), $f = array($sock), 5);<๐> <๐>return(self::$status);<๐> <๐>}<๐> <๐>public function checklist($lst){<๐> <๐>}<๐> <๐>public function status(){<๐> <๐>switch(self::$status)<๐> <๐>{<๐> <๐>case 2:<๐> <๐>echo "Closedn";<๐> <๐>break;<๐> <๐>case 1:<๐> <๐>echo "Openningn";<๐> <๐>break;<๐> <๐>case 0:<๐> <๐>echo "Timeoutn";<๐> <๐>break;<๐> <๐>}<๐> <๐>}<๐> <๐>}<๐> <๐>$ip='192.168.2.10';<๐> <๐>$port=80;<๐> <๐>$health = new Health();<๐> <๐>$health->check($ip, $port); $health->status(); |