socket and file select

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MeLight
Forum Commoner
Posts: 26
Joined: Sun Apr 19, 2009 12:39 pm
Location: Israel

socket and file select

Post by MeLight »

I want to use the select function on a group of sockets AND files like I would in C, but it seems the PHP select only takes sockets. Any idea how to convert file handler to socket, or another workaround?
Thanx
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: socket and file select

Post by Weirdan »

use stream_select(), it works both with socket streams and file streams (it would require you to use specialized stream_socket_* functions to create sockets though).
MeLight
Forum Commoner
Posts: 26
Joined: Sun Apr 19, 2009 12:39 pm
Location: Israel

Re: socket and file select

Post by MeLight »

Thanx! That worked well.
Another question about stream sockets: how would I know that the other side has disconnected?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: socket and file select

Post by Weirdan »

MeLight wrote:Another question about stream sockets: how would I know that the other side has disconnected?
Usually if the socket is selected, but has nothing to read, it's signals it's closed from the other side.

Something like this:

Code: Select all

 
if (select($read, $write, $except, null)) {
     foreach ($read as $sock) {
           $data = read($sock);
           if (!$data) {
                 close($sock);
           }
     }
}
 
MeLight
Forum Commoner
Posts: 26
Joined: Sun Apr 19, 2009 12:39 pm
Location: Israel

Re: socket and file select

Post by MeLight »

ya figured it out eventually, checked if the string is empty. Thanx again :)
Post Reply