Re: Question about socket_select
| From: | Mihai Anghel | Date: | Wed, 14 Dec 2011 11:48:41 +0000 |
| Subject: | Re: Question about socket_select | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-316006@lists.php.net to get a copy of this message | ||
On Wed, Dec 14, 2011 at 1:25 AM, Matijn Woudt <tijnema@gmail.com> wrote:
> On Wed, Dec 14, 2011 at 12:11 AM, Mihai Anghel <mihaigrim1985@gmail.com> wrote:
>> Hello,
>>
>> It appears to me that something is strange with the socket_select function.
>> From what I understand the value of the fourth parameter, tv_sec,
>> should block the execution of the script for that number of seconds.
>> I tried this code :
>> <?php
>>
>> error_reporting(E_ERROR);
>>
>> $serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
>>
>> $result = socket_bind($serverSocket, "127.0.0.1", "20668");
>>
>> $start = time();
>>
>> while(true)
>> {
>> $reads = array($serverSocket);
>> $writes = null;
>> $except = null;
>> $changes = socket_select($reads, $writes, $except, 5);
>> $now = time();
>> echo $now - $start;
>> echo "\n";
>> }
>>
>> and when I run it with php -q server3.php the ouput shows something
>> like 0 0 0 0 0 1 1 1 1 1 2 2 2 2 etc so the script doesn't pause on
>> socket_select until it returns.
>>
>> Cam somebody explain me what's happening ?
>>
>
> It seems to me that your socket_select function is failing, maybe
> because earlier code is failing. Check the return of socket_select
> like this:
> if ($changes === false) {
> echo "socket_select() failed, reason: " .
> socket_strerror(socket_last_error()) . "\n";
> }
>
> Cheers,
>
> Matijn
Thanks for your suggestion, I reviewed the code and I saw that I was
missing : socket_listen($serverSocket) . After adding this it worked
like expected