473,394 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Understanding socket_select routine

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.
From examining the code it seems that if socket_select returns a value

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

Jun 6 '06 #1
4 4948
my********@yahoo.co.uk schreef:
From examining the code it seems that if socket_select returns a value 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.


$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.
$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


Jun 6 '06 #2
my********@yahoo.co.uk wrote:
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.
From examining the code it seems that if socket_select returns a value

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.


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.

Jun 6 '06 #3
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:
my********@yahoo.co.uk wrote:
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.
From examining the code it seems that if socket_select returns a value

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.


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.


Jun 7 '06 #4
my********@yahoo.co.uk wrote:
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?
For practical purpose you're right. You can obviously select() multiple
sockets listening on different ports, but that's not the typical
scenario.
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?


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.

Jun 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: bazad | last post by:
Hi, I'd like to understand consequences of Application.DoEvents call. Does it create a new thread? Thank you
5
by: Tony Johansson | last post by:
Hello Experts! I just play around just to try to understand this about multiple inheritance. You have all the class definition below and at the bottom you have the main program. So here I...
7
by: Morgan | last post by:
I have read much posts on the argument but no one clearly says if this operation is possible or not. Simply I have a routine which reads from a text file some integer arrays (1 or 2D). The...
1
by: Vaelek | last post by:
Hello, I'm somewhat new to DB programming and have a question. I am now taking over an application that a previous programmer had written and he had made use of the Parameter.Add function for DB2...
1
by: everymn | last post by:
Hi, Supposedly the Alter Routine privilege can be granted at the level of a single routine but I haven't been able to get that to work. I've tried it a number of different ways like: GRANT...
2
by: RSH | last post by:
I have been looking at delegates lately. I have seen several articles explaining them but I'm having a hard time understanding why I would use a delegate over a traditional function or sub. Could...
7
by: Bob Darlington | last post by:
I'm using the following routine to call UpdateDiary() - below: Private Sub Form_BeforeUpdate(Cancel As Integer) On Error GoTo Form_BeforeUpdate_Error Call UpdateDiary(Me!TenantCounter,...
6
by: Steven W. Orr | last post by:
Given the following code: (I hope it's as simple as possible) :-) #! /usr/bin/python import new class BASE: def __init__( self ): print 'Hello from BASE init' def m1( self ): print 'M1 Base:...
2
by: pvong | last post by:
I'm a newbie. I'm using VS2008 & VB.net I have a simple site and each page uses the same sub-routine. I copy and paste to each page and that's no big deal but that can get tiresome. I was...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.