Jump to content

Infinate amounts of connections


aximbigfan

Recommended Posts

Hi,

 

I have here the source code to a PHP socket server that is "multithreaded". The issue is that it will only accept up to $max_clients connections.

 

Can anyone see any way to modify this so that it will accept more than $max_clients connections?

 

<?php


while (true) 
{
    
    $read[0] = $sock;
    for ($i = 0; $i < $max_clients; $i++)
    {
        if ($client[$i]['sock']  != null)
            $read[$i + 1] = $client[$i]['sock'] ;
    }
    
    $ready = socket_select($read,null,null,null);
    
    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)
                echo "Client overflow error";
        }
        if (--$ready <= 0)
            continue;
    }
    
    // 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);
            //Stuff here
            }
        } 
	else 
	{
            // Close the socket
            socket_close($client[$i]['sock']);
            unset($client[$i]);
        }
    }
} 
socket_close($sock);
?> 

 

Loops aren't my thing..

 

Thanks,

Chris

Link to comment
https://forums.phpfreaks.com/topic/124978-infinate-amounts-of-connections/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.