PHP Example Method for Checking If a Port Can Be Bound
- 2021-10-27 06:53:11
- OfStack
This article example shows how PHP checks whether a port can be bound. Share it for your reference, as follows:
<?php
/**
* Check whether the port can be bound
* @author flynetcn
*/
function checkPortBindable($host, $port, &$errno=null, &$errstr=null)
{
$socket = stream_socket_server("tcp://$host:$port", $errno, $errstr);
if (!$socket) {
return false;
}
fclose($socket);
unset($socket);
return true;
}
$ret = checkPortBindable('127.0.0.1', 8080, $errno, $errstr);
var_dump($ret, $errno, $errstr);
Run results:
bool(true)
int(0)
string(0) ""
For more readers interested in PHP related contents, please check the topics on this site: "php socket Usage Summary", "php String (string) Usage Summary", "PHP Mathematical Operation Skills Summary", "php Object-Oriented Programming Introduction Tutorial", "PHP Array (Array) Operation Skills Complete Book", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary" and "PHP Network Programming Skills Summary"
I hope this article is helpful to everyone's PHP programming.