萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php實現TCP端口檢測的方法

php實現TCP端口檢測的方法

 本文實例講述了php實現TCP端口檢測的方法。分享給大家供大家參考。具體如下:

該程序可以確認當前端口是否可用:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <?php 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();
copyright © 萬盛學電腦網 all rights reserved