Does no one know how to use pcntl_fork(), then?
;-)


Ben Ramsey wrote:
I'm working with PHP-GTK to create a GUI application. This GUI application opens a socket to a "gaming" server to send/receive data to display to the user. So far, this is working well, but the problem is that it can only receive data after I make a function call to send data.

Here's my program logic so far:

I have the following functions:
enter_key()
send()
receive()
output()

When the user enters text into the text input field, they click their "Enter" key. This calls the enter_key() function. When this function is called, it gets the text from the input field and passes it along to send(), which sends it to the socket. Then enter_key() calls receive(), which receives data from the socket. The receive() function calls the output() function, which displays the received data to the user in a GTK widget.

So far, the program can only receive data from the socket when a call has been made to receive(), and I am explicitly calling receive() after the user clicks the Enter key. This means that data the game is transmitting is not being received until the user enters something.

This is not preferable. What I would like to do is to have a sort of "listener" that listens on the socket and contantly receives data. It has been suggested that I use pcntl_fork() to do this, but I have looked at the manual and user-contributed notes for this, and I can't quite grasp how it's supposed to look in my code.

So far, I've done something along these lines at the very end of my script:

$pid = pcntl_fork();
if ($pid == -1)
{
    die("could not fork");
}
elseif ($pid)
{
    exit(0);
}
else
{
    while (1)
    {
        receive();
    }
}

I'm not sure this is working, though. It does not seem to be receiving from the socket unless I send. Ultimately, I want to remove all calls to receive() from my main program and let the "listener" take control of that, but I can't even tell if the above code is working, or if I'm even grasping how to make it work.

Any help or pointers is greatly appreciated.


-- Regards, Ben Ramsey http://benramsey.com

---------------------------------------------------
http://www.phpcommunity.org/
Open Source, Open Community
Visit for more information or to join the movement.
---------------------------------------------------

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to