ID: 42343
User updated by: mpb dot mail at gmail dot com
Reported By: mpb dot mail at gmail dot com
Status: Closed
Bug Type: Documentation problem
Operating System: Linux/Irrelevant
PHP Version: Irrelevant
Assigned To: nicobn
New Comment:
Hi nicobn,
> The reason socket_set_option() returns NULL is because the parameter
> is not a socket resource
The docs say socket_get_option():
"Returns the value of the given option, or FALSE on errors."
If socket_get_option() is correctly returning NULL, then the docs are
wrong and inaccurate, because the docs do not describe this behavior.
> By the way, all the socket functions return NULL if they are not
> passed proper parameters.
If this is the desired behavior, the docs should reflect it
everywhere. Anything less is inaccurate documentation.
> As for the flawed logic well... it isn't. Socket flags have a
numeric
> value and always had. In this example, when socket_create_listen()
> fails, it returns NULL, not a flag value.
> Also, just fyi, the reason I compare with !== 0 is because the ONLY
> thing that we're sure of is that, if a flag is set, it is going to
be
> non-null and if it is not set, it is going to be 0. Will the actual
> result of socket_get_option be 1 or 122306 ? We don't really know.
You write:
if (socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR) !== 0) {
But FALSE !== 0 is true! And socket_get_option can return FALSE on
error! (And NULL !== 0 is true! And socket_get_option can return NULL
on input errors!)
I claim the following is both functionally correct and more readable:
if (socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR)) {
Thanks for fixing the docs!
-mpb
Previous Comments:
------------------------------------------------------------------------
[2007-08-20 03:59:23] [EMAIL PROTECTED]
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation
better.
Also, just fyi, the reason I compare with !== 0 is because the ONLY
thing that we're sure of is that, if a flag is set, it is going to be
non-null and if it is not set, it is going to be 0. Will the actual
result of socket_get_option be 1 or 122306 ? We don't really know.
------------------------------------------------------------------------
[2007-08-19 22:56:26] [EMAIL PROTECTED]
You are right for the binding part and the documentation will be
changed to reflect this soon. On the other side, the rest of your bug
report is not right.
The reason socket_set_option() returns NULL is because the parameter is
not a socket resource if you run your example twice (because
SO_REUSEADDR was not set, of course). The parameter parsing fails and
NULL is returned. In a real-world situation, you always check if the
resource is not NULL when you pass it as a parameter. By the way, all
the socket functions return NULL if they are not passed proper
parameters. In all cases, if there is an error with the function itself
(and not its parameters), FALSE is returned by this function (you can
look at the source to convince yourself).
As for the flawed logic well... it isn't. Socket flags have a numeric
value and always had. In this example, when socket_create_listen()
fails, it returns NULL, not a flag value.
The only bug is the example, thank you for the heads up, but that's not
"a lot of bugs".
------------------------------------------------------------------------
[2007-08-19 21:50:21] mpb dot mail at gmail dot com
Description:
------------
On this page:
http://www.php.net/manual/en/function.socket-set-option.php
Example 2275. socket_set_option() example
The example is misleading. To be useful, SO_REUSEADDR must be set
before bind. However, socket_create_listen calls bind (internally)
before SO_REUSEADDR is set.
So, if you:
1) run the (modified) script (the Reproduce code)
2) connect (via another process, say a manual telnet)
3) (the script will exit after socket_accept)
4) then re-run the script
In step 4, socket_create_listen will fail, because SO_REUSEADDR is NOT
set when socket_create_listen is called.
So, basically, unless socket_create_listen *automatically* and
*invisibly* sets SO_REUSEADDR for us, we cannot meaningfully use
SO_REUSEADDR in combination with socket_create_listen.
Probably socket_create_listen should be extend with another (boolean)
parameter so that we can tell it whether or not to set SO_RESUEADDR.
But, at the very least the docs should be fixed.
Additionally, the logic is flawed. On error, socket_get_option returns
NULL, and (NULL !== 0) is true, so the example prints "SO_REUSEADDR is
set on socket !", even though there is *no* socket!.
Additionally, the docs say that socket_get_option should return false
on error, but on my machine (PHP 5.2.3) it returns NULL.
That's a lot of bugs!
Reproduce code:
---------------
<?php
$socket = socket_create_listen(1223);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
if (socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR) !== 0) {
echo 'SO_REUSEADDR is set on socket !' . PHP_EOL;
}
// I add the following line so we can actually connect.
socket_accept ($socket);
?>
Expected result:
----------------
SO_REUSEADDR is set on socket !
Actual result:
--------------
PHP Warning: socket_create_listen(): unable to bind to given adress
[98]: Address already in use in <snip>/test.php on line 2
PHP Warning: socket_set_option() expects parameter 1 to be resource,
boolean given in <snip>/test.php on line 3
PHP Warning: socket_get_option() expects parameter 1 to be resource,
boolean given in <snip>/test.php on line 5
SO_REUSEADDR is set on socket !
PHP Warning: socket_accept() expects parameter 1 to be resource,
boolean given in <snip>/test.php on line 10
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=42343&edit=1