#42343 [NEW]: socket_set_option code example is misleading
| From: | mpb dot mail at gmail dot com | Date: | Sun, 19 Aug 2007 21:50:21 +0000 |
| Subject: | #42343 [NEW]: socket_set_option code example is misleading | ||
| Groups: | php.doc | ||
| Request: | Send a blank email to phpdoc+get-969377731@lists.php.net to get a copy of this message | ||
From: mpb dot mail at gmail dot com
Operating system: Linux/Irrelevant
PHP version: Irrelevant
PHP Bug Type: Documentation problem
Bug description: socket_set_option code example is misleading
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 bug report at http://bugs.php.net/?id=42343&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=42343&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=42343&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=42343&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=42343&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=42343&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=42343&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=42343&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=42343&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=42343&r=support
Expected behavior: http://bugs.php.net/fix.php?id=42343&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=42343&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=42343&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=42343&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=42343&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=42343&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=42343&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=42343&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=42343&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=42343&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=42343&r=mysqlcfg