Connecting Tech Pros Worldwide Help | Site Map

Understanding socket_select routine

mydejamail@yahoo.co.uk
Guest
 
Posts: n/a
#1: Jun 6 '06
I have begun dabbling in php socket programming, and I have been trying
to get the hang of the socket_select command based on the example here
- http://www.zend.com/pecl/tutorials/sockets.php.
[color=blue]
>From examining the code it seems that if socket_select returns a value[/color]
of >= 2 and $sock is find in the $read array will the code flow on to
the reading portion. What is the significance of having a value of >=2
in the $ready variable.

I know that this may be due to some lack of familiarity with sockets
and PHPs approach to them, but can anyone enlighten me on the value in
$ready variable and how the $read array passed to the socket_select
variable is modified?

while (true) {
// Setup clients listen socket for reading
$read[0] = $sock;
for ($i = 0; $i < $max_clients; $i++)
{
if ($client[$i]['sock'] != null)
$read[$i + 1] = $client[$i]['sock'] ;
}
// Set up a blocking call to socket_select()
$ready = socket_select($read,null,null,null);
/* if a new connection is being made add it to the client array */
if (in_array($sock, $read)) {
for ($i = 0; $i < $max_clients; $i++)
{
if ($client[$i]['sock'] == null) {
$client[$i]['sock'] = socket_accept($sock);
break;
}
elseif ($i == $max_clients - 1)
print ("too many clients")
}
if (--$ready <= 0)
continue;
} // end if in_array

Sjoerd
Guest
 
Posts: n/a
#2: Jun 6 '06

re: Understanding socket_select routine


mydejamail@yahoo.co.uk schreef:[color=blue][color=green]
> >From examining the code it seems that if socket_select returns a value[/color]
> of >= 2 and $sock is find in the $read array will the code flow on to
> the reading portion. What is the significance of having a value of >=2
> in the $ready variable.[/color]

$ready contains the number of sockets on which the state has changed.
The code which you posted goes through the loop $ready times, accepting
$sock and putting the result in an empty $client[$i]['sock'].

This is a hard example. It is probably best to try out socket_select
with just one socket. Try what it does and what its return value is.
[color=blue]
> $ready = socket_select($read,null,null,null);
> /* if a new connection is being made add it to the client array */
> if (in_array($sock, $read)) {
> for ($i = 0; $i < $max_clients; $i++)
> {
> if ($client[$i]['sock'] == null) {
> $client[$i]['sock'] = socket_accept($sock);
> break;
> }
> elseif ($i == $max_clients - 1)
> print ("too many clients")
> }
> if (--$ready <= 0)
> continue;
> } // end if in_array[/color]

Chung Leong
Guest
 
Posts: n/a
#3: Jun 6 '06

re: Understanding socket_select routine


mydejamail@yahoo.co.uk wrote:[color=blue]
> I have begun dabbling in php socket programming, and I have been trying
> to get the hang of the socket_select command based on the example here
> - http://www.zend.com/pecl/tutorials/sockets.php.
>[color=green]
> >From examining the code it seems that if socket_select returns a value[/color]
> of >= 2 and $sock is find in the $read array will the code flow on to
> the reading portion. What is the significance of having a value of >=2
> in the $ready variable.[/color]

In the example, socket_select() is used to watch for two types of
events: new connections and availability of data. $sock is different
from the other sockets in the array in that it's not really connected
to anything. It's sort of a placeholder for a new socket. When it
becomes readable, it means there's a new incoming connection, and you
call socket_accept() to get the actual socket for that new connection.

So $ready is the number of new connections (1 or 0) plus the number of
sockets with readable data. If it's 1 and the socket is $sock, then
there are no sockets with readable data.

mydejamail@yahoo.co.uk
Guest
 
Posts: n/a
#4: Jun 7 '06

re: Understanding socket_select routine


This appears to imply that socket_select can only notify just 1 new
socket, and that if more than 1 new connections have been made, the
additional connections can only be detected on the next pass - is it
so?

Concerning the $sock value, the $read[0] = $sock at the top of the loop
always makes $sock an element of the array, so I would expect
in_array($sock, $read) to always return true. So I would take it that
socket_select removes or modifies $read[0], or any instances of $read
where $read[x] = $sock if there is no new connection, right?


Chung Leong wrote:[color=blue]
> mydejamail@yahoo.co.uk wrote:[color=green]
> > I have begun dabbling in php socket programming, and I have been trying
> > to get the hang of the socket_select command based on the example here
> > - http://www.zend.com/pecl/tutorials/sockets.php.
> >[color=darkred]
> > >From examining the code it seems that if socket_select returns a value[/color]
> > of >= 2 and $sock is find in the $read array will the code flow on to
> > the reading portion. What is the significance of having a value of >=2
> > in the $ready variable.[/color]
>
> In the example, socket_select() is used to watch for two types of
> events: new connections and availability of data. $sock is different
> from the other sockets in the array in that it's not really connected
> to anything. It's sort of a placeholder for a new socket. When it
> becomes readable, it means there's a new incoming connection, and you
> call socket_accept() to get the actual socket for that new connection.
>
> So $ready is the number of new connections (1 or 0) plus the number of
> sockets with readable data. If it's 1 and the socket is $sock, then
> there are no sockets with readable data.[/color]

Chung Leong
Guest
 
Posts: n/a
#5: Jun 7 '06

re: Understanding socket_select routine


mydejamail@yahoo.co.uk wrote:[color=blue]
> This appears to imply that socket_select can only notify just 1 new
> socket, and that if more than 1 new connections have been made, the
> additional connections can only be detected on the next pass - is it
> so?[/color]

For practical purpose you're right. You can obviously select() multiple
sockets listening on different ports, but that's not the typical
scenario.
[color=blue]
> Concerning the $sock value, the $read[0] = $sock at the top of the loop
> always makes $sock an element of the array, so I would expect
> in_array($sock, $read) to always return true. So I would take it that
> socket_select removes or modifies $read[0], or any instances of $read
> where $read[x] = $sock if there is no new connection, right?[/color]

That is correct. Hence the reference declaration for the arguments. PHP
typically doesn't do things this way. The semantic came from the socket
functions in C.

Closed Thread