//exit(0);
// first we want to fork into the background like all good daemons should
//$pid = pcntl_fork();
+
+// uncomment this bit and comment the fork above to stop it going into the background
$pid = 0;
if($pid == -1) {
echo "i am a parent, i leave\n";
exit(0);
} else {
+ // here is where i need to swithc to TCP network protocol stuff
+ // i must bind 127.0.0.1 though.
+ // what i want to happen is this:
+ // 1) server receives connection
+ // 2) server forks off process to process connection
+ // 3) main server continues.
+ // a forked process thingy should be fully self contained and capable of dealing
+ // with "problems", i.e. the parent doesnt want to have to clean up children
global $MSG_QUEUE_KEY_ID_SERVER, $MSG_QUEUE_KEY_ID_CLIENT;
$cl_queue = msg_get_queue($MSG_QUEUE_KEY_ID_CLIENT, 0666 | 'IPC_CREAT');
// this functiuon will now act as our generic send/recieve client funciton
// im doing this because im going to move from ipc messaging to a tcp connection
// shortly and i want to encapsulate the send/receive behaviour
+ // things we need to add here are:
+ // 1) a way of saying "more data coming" cause getusers wont fit into one message
+ // 2) timeouts and locking
function sendReceive($message_type, $message) {
global $MSG_QUEUE_KEY_ID_SERVER, $MSG_QUEUE_KEY_ID_CLIENT;
--- /dev/null
+<?php
+// a test of binding
+$res = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+
+socket_bind($res, "127.0.0.1", 10051);
+while(true) {
+ $t = socket_listen($res);
+ $res2 = socket_accept($res);
+ echo "I went past listen\n";
+ $i = pcntl_fork();
+ if($i == -1) echo "Failed to fork\n";
+ else if (!$i) {
+ // i am a child
+ echo "Child processing\n";
+ while(true) {
+ socket_send($res2, "stuff\n", 6, 0);
+ $str = "";
+ echo "Child wait data\n";
+ $k = socket_recv($res2, $str, 16, MSG_WAITALL);
+ echo "Child got data\n";
+ socket_send($res2, $str, $k, 0);
+ }
+ }
+}
+
+?>