Page 1 of 1

socket and file select

Posted: Wed Jul 22, 2009 9:16 am
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

Re: socket and file select

Posted: Wed Jul 22, 2009 1:05 pm
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).

Re: socket and file select

Posted: Thu Jul 23, 2009 6:50 am
by MeLight
Thanx! That worked well.
Another question about stream sockets: how would I know that the other side has disconnected?

Re: socket and file select

Posted: Thu Jul 23, 2009 10:29 am
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);
           }
     }
}
 

Re: socket and file select

Posted: Thu Jul 23, 2009 11:47 am
by MeLight
ya figured it out eventually, checked if the string is empty. Thanx again :)