Jump to content

The select_socket() Function


iwato
 Share

Recommended Posts

BACKGROUND: The socket_select() function appears to be useful when you have more than one socket open. Apparently, the function allows you to read and write to multiple files accessed via multiple sockets on multiple servers. Though likely very efficient, this ability appears to be contingent on a particular option called blocking/nonblocking. I am not particularly clear about how this option works.REQUEST for INTERPRETATION: The following passage was taken from the webpage devoted to the socket_select() function. It can be found under the $read parameter heading. I simply do not understand the part enclosed in parentheses.

The sockets listed in the read array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a socket resource is also ready on end-of-file, in which case a socket_read() will return a zero length string).
Roddy
Link to comment
Share on other sites

Blocking means that if one thing is reading the stream, something else can't write to it at the same time. That is called a blocking read. When it says the sockets will be watched to see if characters become available for reading, that means it's checking to see if it can do any non-blocking reads, or if it can read the data without needing to block writes. It also says that if the character in the socket is an end-of-file character, the socket will be ready to read without a block, but reading it will return no data. At least, that's how I read it.

Link to comment
Share on other sites

Blocking means that if one thing is reading the stream, something else can't write to it at the same time. That is called a blocking read. When it says the sockets will be watched to see if characters become available for reading, that means it's checking to see if it can do any non-blocking reads, or if it can read the data without needing to block writes. It also says that if the character in the socket is an end-of-file character, the socket will be ready to read without a block, but reading it will return no data. At least, that's how I read it.
So, if I have understood properly, as soon as a block is lifted, the stream becomes available for other operations different from the current operation. What I do not understand is how PHP knows that the block has been lifted.
  1. Is the block automatically lifted when the current operation is completed?
  2. If so, how is the lifting of the block communicated to the listening object?
  3. Finally, what is the listening object?

Roddy

Link to comment
Share on other sites

The block being lifted is explicit:http://www.php.net/manual/en/function.socket-set-block.phphttp://www.php.net/manual/en/function.sock...et-nonblock.phpOther than setting a flag for the socket, I'm not sure the technical details of how it gets communicated. The system is listening on the socket and watching for events, and that's one of the events that happen. The "listening object" is the server.

Link to comment
Share on other sites

The "listening object" is the server.
Hmmmmm. I will explore the links that you provided, ...In the meantime I have another related question.COMMENT: I have noticed that I cannot use the socket_create() question on my virtual hosts. This said, I can open as many sockets that I wish by simply changing the port at the generic address 127.0.0.1. QUESTION: Is this because you can have only one socket per port? Or, does there exist something like a permanent or dedicated socket? Roddy
Link to comment
Share on other sites

Right, one socket per port. When your web server is listening on port 80 it has a socket open and bound to that port, so nothing else can open a socket on port 80 and the web server will keep listening on it until you stop the server.

Link to comment
Share on other sites

Right, one socket per port. When your web server is listening on port 80 it has a socket open and bound to that port, so nothing else can open a socket on port 80 and the web server will keep listening on it until you stop the server.
Or, close the socket. Is this correct?Also, I am confused by the roles of the socket_getsockname() and socket_getpeername(). The first applies to the local side of the socket and the second applies to the remote side of the socket. What is meant by local and remote in this context?Does it mean incoming (local) and outgoing (remote) traffic on the same socket?Does it mean the server socket (local) and a different socket at the other end of the connection (remote) -- e.g. a socket at a different port on the same machine, or possibly on another machine on the same LAN or different IP address altogether?Roddy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...