After hours of pulling my hair out and staring a C code I don't understand, 
I almost have a generic PHP multiplexing TCP server running. Basically 
right now multiple clients can connect and be recognized by the server and 
the server is aware of incoming input from the clients.

This is mostly using undocumented functions from the Socket extension and I 
could really use some help from anyone that knows sockets better than I.

 From where I am, I still need to figure out when a client disconnects, 
plus how to disconnect a client (at the server's whim) directly. I also 
need the multiplex to be aware of writes as well as reads, although I think 
I should be able to sort that part out, it's the disconnects that I haven't 
a clue on yet.

Here's my sloppy code (sorely lacking in error checking, but oh well):

// PHP normally times out, we don't want to do that here.
set_time_limit (0);
// Server's address and port we want to listen to.
$address = '209.53.13.112';
$port = 8080;
// Start the socket.
$sock = socket (AF_INET, SOCK_STREAM, 0);
$foo = bind ($sock, $address, $port);
$foo = listen ($sock, 10);
// Allocate File Descriptor, not sure if this is needed, I'm just guessing.
$fdgroup = fd_alloc();
$maxfd = $sock + 1;
do {
        // FD needs to be rebuilt each time because select() may alter it.
        if ($maxfd > $sock + 1) {
                // If any clients have connected, rebuild the FD from the list.
                $foo = fd_zero($fdgroup);
                $foo = fd_set($sock,$fdgroup);
                for ($i=0;$i <= count($fdlist);$i++) {
                        $foo = fd_set($fdlist[$i],$fdgroup);
                }
        } else {
                // or else the socket itself is the only FD.
                $foo = fd_zero($fdgroup);
                $foo = fd_set($sock,$fdgroup);
        }
        // Now the very important and magical select(), set to timeout after a 
million microseconds (1 second).
        $selectnow = select($maxfd, $fdgroup, NULL, NULL, 0, 1000000);
        if ($selectnow == 0) {
                // We've timed out, put any appropriate activities here.
        } elseif ($selectnow > 0) {
                for ($fd=$sock;$fd <=$maxfd;$fd++) {
                        if (fd_isset($fd,$fdgroup)) {
                                if ($fd == $sock) {
                                        // The socket is knocking, someone is at the 
door.
                                        $newfd = accept_connect($sock);
                                        if ($newfd > 0) {
                                                $fdlist[] = $newfd;
                                                if ($maxfd < $newfd + 1) $maxfd = 
$newfd + 1;
                                        }
                                } else {
                                        // One of our clients has something to say.
                                }
                        }
                }
        }
} while (true);
// Do we need to deallocate File Descriptors? I'm not sure.
$foo = fd_dealloc($fdgroup);
close ($sock);



--
                     Visit the Gates Motel webgame:
                     http://www.gameslate.com/gatesmotel/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to