note 106006 deleted from function.socket-create-listen by danbrown
| From: | danbrown@php.net | Date: | Mon, 03 Oct 2011 13:12:52 +0000 |
| Subject: | note 106006 deleted from function.socket-create-listen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183604@lists.php.net to get a copy of this message | ||
Note Submitter: w_barath at hotmail dot com
----
re vicente dot lopez at caixadesoft dot es's post below
The reason only the last port remains bound is because of PHP's automatic garbage collection.
$sock is a reference to a resource. When you re-use $sock then the refcount on the resource drops to
zero and it is destroyed.
This is great because we can write very simple and lazy code, but we also have to be careful to
preserve the resources we mean to keep using:
foreach($services as $port => $daemon){
if ($sock = @socket_create_listen($port)){
//$sockets[$sock] = $daemon; // using $sock as a key does not preserve it.
$sockets[$daemon] = $sock; // saving it as a value does!
} else {
echo("error\n");
}
}