Accept only single connection

I am creating a socket script that will listen to our 3 devices.and the device is setup in one server ip and one port only.


 $file = fopen('txt.log','a+');
    $server = stream_socket_server('tcp://'.$ipserver.':'.$port, $errno, $errorMessage);

    if(!$server) {
        echo "$errorMessage ($errno)<br />\
";
    }
    else{

        while($client = @stream_socket_accept($server,$timeout)) {
               stream_copy_to_stream($client, $file);
               fclose($file);
               fclose($client);


        }

    }

but the problem is that if one device is connected,the two devices cannot connect anymore.I appreciate some one can help me how to get this work.

Thank you in advance.

Hi i am using stream_select to accept my server with multiple connection.but i am having problem in reading the data that was sent from the device.


 $file = fopen('txt.log','a+');
	
	
    $server = stream_socket_server('tcp://'.$ipserver.':'.$port, $errno, $errorMessage);

    if(!$server) {
	   
         echo " $errorMessage ($errno)\
";
    }
    else{
          
		   $read   = array($server);
           $write  = NULL;
           $except = NULL;
		   
		 
        while($num_changed_streams = @stream_select($read, $write, $except, $timeout)) {
             $line = fread($server,1024); //here i don't know how to read the stream
			 echo $line;
        }
	  
     

      fclose($server);
    }
	
	

thank you in advance.

You can check this example to serve many clients

<?php

// Set time limit to indefinite execution

set_time_limit (0);

// Set the ip and port we will listen on

$address = '192.168.0.100';

$port = 9000;

$max_clients = 10;

// Array that will hold client information

$clients = 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) {

    // Setup clients listen socket for reading

    $read[0] = $sock;

    for ($i = 0; $i < $max_clients; $i++)

    {

        if ($client[$i]['sock']  != null)

            $read[$i + 1] = $client[$i]['sock'] ;

    }

    // Set up a blocking call to socket_select()

    $ready = socket_select($read,null,null,null);

    /* 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 ($client[$i]['sock'] == null) {

                $client[$i]['sock'] = socket_accept($sock);

                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 (in_array($client[$i]['sock'] , $read))

        {

            $input = socket_read($client[$i]['sock'] , 1024);

            if ($input == null) {

                // Zero length string meaning disconnected

                unset($client[$i]);

            }

            $n = trim($input);

            if ($input == 'exit') {

                // requested disconnect

                socket_close($client[$i]['sock']);

            } elseif ($input) {

                // strip white spaces and write back to user

                $output = ereg_replace("[ \	\
\\r]","",$input).chr(0);

                socket_write($client[$i]['sock'],$output);

            }

        } else {

            // Close the socket

            socket_close($client[$i]['sock']);

            unset($client[$i]);

        }

    }

} // end while

// Close the master sockets

socket_close($sock);

?>

I could not get all my clients to connect to our server in socket,it only connect one client at a time when a new connection made.
can you help me on this please.



// 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...\
");
                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\
");
                unset($client[$i]);
				fclose($file);
            }
            $n = trim($input);
            if ($n == 'exit') {
                echo("Client requested disconnect\
");
                // requested disconnect
                socket_close($client[$i]['sock']);
            }
            elseif ($input) {
                echo("Receiving data\
");
                // strip white spaces and write back to user
                $output = ($input);
                //socket_write($client[$i]['sock'],$output);
				 fwrite($file,$output);
				 echo $output."\\r\
";

				
            }
        } else {
            // Close the socket
            if (isset($client[$i]))
            echo("Client disconnected\
");
			
            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\
");
socket_close($sock);
fclose($file);