HIHI,
My server has the choice to create multiple sockets to different host when commands is sent to the main socket ; $socket[0].
This is my code
global $i, $socket;
$socket[0] = @fsockopen($ip, $port, &$errno, &$errstr, 2);
// The Loop
do {
for ($i=0; $i < 2; $i++) {
if (is_null($socket[$i])) {
continue;
}
$read = trim(fgets($socket[$i], 1028));
if (!$read == trim($read)) {
continue;
}
$read = trim ($read);
// Perform this if its the main socket
if ($i == 0) {
$params = explode(" ", $read[res]);
// when create is called
if ($params[1] == "create") {
// Loop to make sure socket not in used
for ($j = 0; $j <10; $j ++) {
// if socket is not in used.
if ($socket[$j] == NULL) {
//create socket and set $j to 11 to get out of loop
$socket[$j] = @fsockopen($params[2], $params[3], &$errno, &$errstr, 2);
$j = 11;
}
}
}
//if help is called
if ($params[1] == "help") {
server("HELP COMMAND IS CALLED!!!!!");
}
}
// if its not the main socket, it will just print hello world
server("hello world");
}
}while(true);
function server($raw) {
global $i;
fputs($socket[$i],$raw, str_len($raw));
}
The problem is "hello world" will not be printed out unless $socket[0] has recieved some datas, like create or Help.
It seems that the loop will skip all the other sockets unless the main socket is read. When the main is read, for example HELP is called, den HEllO World would be printed.
Can anyone see any errors in my code that causes that? Can anyone give me any advise?
Also I wish to implement the if (!feof($socket[$i]) test in the loop, where do u think i should put it, because I wish to close $socket[$i] once the destination host close the connection, so that $socket[$i] would be set to null and be used again....
Any helps would be greatly appreciated. Thank u
PS: Forgot to add that, once $socket[0]'s destination host died off, the other socks will den start to print "HELLO WORLD" non stop. Is there anything wrong with implementing $socket[0] as main socket?