Page 1 of 1

Loop question

Posted: Tue Jun 29, 2010 10:07 pm
by Payton
Okay, so here's what I'm trying to do:

Using a file that stores an array of connection data with their respective ID(s), I want to start a socket connection. However, if the default connection fails, I want there to be a loop that adds one to the most recent connection's ID, and then attempts to reconnect. Only problem is that I am stumped as to how I would do the looping. :?

This is the file that stores the connection information (servers.php):

Code: Select all

<?php

/* Connection data */

$servers = array (
	100 = array (
		'server_ip' => '192.168.0.12',
		'server_port' => 7100
	),
	
	101 = array (
		'server_ip' => '192.168.0.12',
		'server_port' => 7101
	)
);

?>
And this is the file that actually handles the connection:

Code: Select all

<?php

require("servers.php");
require("config.php");

if(!defined(SHUTDOWN)) {
	set_time_limit(0);
	 
	$sock = socket_create(AF_INET, SOCK_STREAM, 0); 
	
	$bind = socket_bind($sock, $server[100]['server_ip'], $server[100]['server_port']);
	
	if(!$bind) {
		//The loop should go here	
	}
	
	socket_listen($sock); 
	
	//Everything else
}

elseif(defined(SHUTDOWN)) {
	//things to do if the server is offline 
}

?>
Obviously it should just be a basic for loop, such as this:

Code: Select all

for($i=100; $i<=101; $i++) {
          //connect here using $servers[$i]['server_ip'] and then the port
}
But wouldn't that just connect to every other server listed as well? Say if I had 102, 103, 104, and 105 set up as well... the loop would connect to all five, right? How would I connect to just 101 if 100 failed, and then stop the loop (and so on and so forth, assuming 101 failed too)?

Re: Loop question

Posted: Wed Jun 30, 2010 12:18 am
by requinix
Don't use a for loop. Just a simple foreach that breaks out if the connection works.

Code: Select all

$connected = false;
foreach ($servers as $server) {
    // do whatever
    // if (it connected and everything worked) {
        $connected = true;
        break;
    // }
}

if ($connected) {
    // all good
} else {
    // ran out of servers to check
}

Re: Loop question

Posted: Wed Jun 30, 2010 12:43 am
by Payton
tasairis wrote:Don't use a for loop. Just a simple foreach that breaks out if the connection works.

Code: Select all

$connected = false;
foreach ($servers as $server) {
    // do whatever
    // if (it connected and everything worked) {
        $connected = true;
        break;
    // }
}

if ($connected) {
    // all good
} else {
    // ran out of servers to check
}
So, is this correct?

Code: Select all

<?php

require("servers.php");
require("config.php");

if(!defined(SHUTDOWN)) {
	set_time_limit(0);
	
	$connected = false;
	
	foreach ($servers as $server) {
		$sock = socket_create(AF_INET, SOCK_STREAM, 0);
		$bind = socket_bind($sock, $server[100]['server_ip'], $server[100]['server_port']);
		
		if($bind) {
			$connected = true;
			break;
		}
	}

	if ($connected = true) {
		//everything	
	} 

	else {
		// ran out of servers to check
	}
}

elseif(defined(SHUTDOWN)) {
	//things to do if the server is offline 
}

?>
Thanks for the help!

Re: Loop question

Posted: Wed Jun 30, 2010 9:03 am
by AbraCadaver
defined() takes a string argument, not a constant. Unless of course the constant defines a string which is actually the name of another constant that you want to check.

Re: Loop question

Posted: Wed Jun 30, 2010 1:03 pm
by Payton
Ah, right, thanks for pointing that out to me.