How to accept multiple connection to client

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jemz
    New Member
    • Jul 2014
    • 1

    #1

    How to accept multiple connection to client

    Hi, I am having trouble on how can i accept multiple connection from my device (client)..my socket code will only accept single connection from client.and if it is disconnected it will accept another client one at time,but i want all my client can send data to our server or can connect to my server.


    Code:
     
    // Set time limit to indefinite execution
    set_time_limit (0);
    // Set the ip and port we will listen on
    $address = '192.168.0.11';//server ip demo only.
    $port = 123;
    $max_clients = 10;
    // Array that will hold client information
    $client = array();
    // Create a TCP Stream socket
    $sock = socket_create(AF_INET, SOCK_STREAM, 0);
    // Bind the socket to an address/port
    socket_bind($sock, $address, $port) or die('Could not bind to address');
    // Start listening for connections
    socket_listen($sock);
    // Loop continuously
    while (true) {
       $file = fopen('txt.log','a'); 
        // Setup clients listen socket for reading
        $read[0] = $sock;
        for ($i = 0; $i < $max_clients; $i++)
        {
            if (isset($client[$i]))
            if ($client[$i]['sock']  != null)
                $read[$i + 1] = $client[$i]['sock'] ;
        }
        // Set up a blocking call to socket_select()
    	$write = null;
        $except = NULL;
    	$tv_sec = NULL;
    	echo "connecting";
        $ready = socket_select($read,$write,$except,$tv_sec);
        /* if a new connection is being made add it to the client array */
        if (in_array($sock, $read)) {
            for ($i = 0; $i < $max_clients; $i++)
            {
                if (!isset($client[$i])) {
    			  
                    $client[$i] = array();
    				 socket_set_nonblock($sock); 
                    $client[$i]['sock'] = socket_accept($sock);
    				  $file = fopen('txt.log','a'); 
                    
                    echo("Accepting incomming connection...\n");
                    break;
                }
                elseif ($i == $max_clients - 1)
                    print ("too many clients");
            }
            if (--$ready <= 0)
                continue;
        } // end if in_array
    
        // If a client is trying to write - handle it now
        for ($i = 0; $i < $max_clients; $i++) // for each client
        {
            if (isset($client[$i]))
            if (in_array($client[$i]['sock'] , $read))
            {
                $input = socket_read($client[$i]['sock'] , 1024);
                if ($input == null) {
                    // Zero length string meaning disconnected
                    echo("Client disconnected\n");
                    unset($client[$i]);
    				fclose($file);
                }
                $n = trim($input);
                if ($n == 'exit') {
                    echo("Client requested disconnect\n");
                    // requested disconnect
                    socket_close($client[$i]['sock']);
                }
                elseif ($input) {
                    echo("Receiving data\n");
                    // strip white spaces and write back to user
                    $output = ($input);
                    //socket_write($client[$i]['sock'],$output);
    				 fwrite($file,$output);
    				 echo $output."\r\n";
                    
    				 
                }
            } else {
                // Close the socket
                if (isset($client[$i]))
                echo("Client disconnected\n");
    			
                if ($client[$i]['sock'] != null){ 
                    socket_close($client[$i]['sock']); 
                    unset($client[$i]); 
    			
    				
                }
    			
            }
    		
        }
    	fclose($file);//closing file
    } // end while
    // Close the master sockets
    echo("Shutting down\n");
    socket_close($sock);
    fclose($file);
    Thank you in advance.
Working...