Hi all. I'm trying to detect if ports are unused on the server so I can set it up, do my stuff, then close it again - I'm wondering if this method using fsockopen is a really rubbish - I'm quite hoping for a right slap on this as it's just brutal door knocking - but I'd also welcome "Yeah, that's probably the way I'd do it" too.
Cheers.
function find_unused_socket()
{
$port=9050;
$portLimit=$port + 100;
$freePort=0;
while($port < $portLimit )
{
$fp=@fsockopen('127.0.0.1', $port, $errno, $errstr, .005);
if( $fp )
{
echo $errno.' '; echo "$port in use";
fclose($fp);
}
else
{
echo $errno.' ' ; echo "$port free";
$freePort=$port;
}
echo '<br>';
$port++;
}
return $freePort;
}