dewmat Posted December 14, 2007 Share Posted December 14, 2007 Its always sending 2 times test to everyone.. Why? I want him to send it only once to all of them... #!/usr/bin/php -q <?php error_reporting(E_ALL); set_time_limit(0); ob_implicit_flush(); $address = '192.168.2.3'; $port = 8888; function handle_client($allclient) {//write to all foreach($allclient as $client) { socket_write($client, "test"); /////////////////////////////////////////////////////////////////////////He is sending "test" 2times why? } } if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) { echo "socket_create() failed: reason: " . socket_strerror($master) . "\n"; } socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1); if (($ret = socket_bind($master, $address, $port)) < 0) { echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n"; } if (($ret = socket_listen($master, 5)) < 0) { echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n"; } $read_sockets = array($master); while (true) { $changed_sockets = $read_sockets; $num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL); foreach($changed_sockets as $socket) { if ($socket == $master) { if (($client = socket_accept($master)) < 0) { echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n"; continue; } else { array_push($read_sockets, $client); } } else { if (false === ($buf = socket_read($socket, 2048, PHP_NORMAL_READ))) { echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n"; $index = array_search($socket, $read_sockets); unset($read_sockets[$index]); socket_close($socket); } else { if (!$buf = trim($buf)); if ($buf == 'quit') { $index = array_search($socket, $read_sockets); unset($read_sockets[$index]); socket_close($socket); }elseif ($buf == 'shutdown') { socket_close($master); break 2; }else{ //write enkel deze $allclients = $read_sockets; array_shift($allclients); // remove master handle_client($allclients); } } } } } ?> Quote Link to comment Share on other sites More sharing options...
dewmat Posted December 14, 2007 Author Share Posted December 14, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
dewmat Posted December 14, 2007 Author Share Posted December 14, 2007 Does someone know why always sends everything 2 times? Quote Link to comment Share on other sites More sharing options...
dewmat Posted December 14, 2007 Author Share Posted December 14, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
dewmat Posted December 14, 2007 Author Share Posted December 14, 2007 someone? please... Quote Link to comment Share on other sites More sharing options...
dewmat Posted December 15, 2007 Author Share Posted December 15, 2007 i replaced function(handle_client) with thisone: function handle_client($allclient) { //write to all print_r($allclient); } He prints 2 times the array.. The array is correct but why is the function handle_client called twice? Quote Link to comment Share on other sites More sharing options...
dewmat Posted December 15, 2007 Author Share Posted December 15, 2007 foreach($changed_sockets as $socket) { echo "how many times"; This one echo's how many times twice to... SO i think here is the problem but how to solve it? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.