Jump to content

socket problem


dewmat

Recommended Posts

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);

               }

           }

       }    

   }

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.