ID: 42343 Updated by: [EMAIL PROTECTED] Reported By: mpb dot mail at gmail dot com -Status: Open +Status: Assigned Bug Type: Documentation problem Operating System: Linux/Irrelevant PHP Version: Irrelevant -Assigned To: +Assigned To: nicobn New Comment:
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". Previous Comments: ------------------------------------------------------------------------ [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
