Jump to content

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

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.