Home » Php » PHP TCP socket connection Limit – Windows server

PHP TCP socket connection Limit – Windows server

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have a weird issue and I can’t seem to find a solution or anything closer to the issue I am having ,

Here is the thing , I have a scoket script run via php on command line, it accepts connection and reads data in json format from mobile app clients and sends appropriate response in json.

Everything works fine except the number of connection does not go above 256 connection.

I would like to know why is that, and how can I solve it ? I have been It on so many days, but no luck!

Here is the script snippet

<?php

date_default_timezone_set("UTC");
$server = stream_socket_server("tcp://192.168.1.77:25003", $errno, $errorMessage);

if (!$server) {
    die("$errstr ($errno)");
}

echo "Server started..";
echo "\r\n";

$client_socks = array();

while (true) {
    //prepare readable sockets
    $read_socks = $client_socks;
    $read_socks[] = $server;

    //start reading and use a large timeout
    if (!stream_select ($read_socks, $write, $except, 10000)) {
        die('something went wrong while selecting');
    }

    //new client
    if (in_array($server, $read_socks)) {
        $new_client = stream_socket_accept($server);

        if ($new_client) {
            //print remote client information, ip and port number
            echo 'Connection accepted from ' . stream_socket_get_name($new_client, true);
            echo "\r\n";

            $client_socks[] = $new_client;
            echo "Now there are total ". count($client_socks) . " clients";
            echo "\r\n";
        }        

        //  echo stream_socket_get_name($new_client, true);
        //delete the server socket from the read sockets
        unset($read_socks[array_search($server, $read_socks)]);
    }

    $data = '';
    $res = '';

    //message from existing client
    foreach($read_socks as $sock) {
        stream_set_timeout($sock, 1000); 
        while($resp = fread($sock, 25000)) {
           $data .= $resp;
           if (strpos($data, "\n") !== false) {
                break;
            }
        }

        $info = stream_get_meta_data($sock);    

        if ($info['timed_out']) {
            unset($client_socks[array_search($sock, $client_socks)]);
            @fclose($sock);  
            echo 'Connection timed out!';
            continue;
        }       

        $client = stream_socket_get_name($sock, true);

        if (!$data) {
            unset($client_socks[array_search($sock, $client_socks)]);
            @fclose($sock);           

            echo "$client got disconnected";
            echo "\r\n";
            continue;
        }

        //send the message back to client
        $decode = json_decode($data);


        $encode = json_encode($res);   
        fwrite($sock,$encode."\n");      
    } 
}

P.S.: What I did is, extensive search on the topic, and went over article like these,
http://smallvoid.com/article/winnt-tcpip-max-limit.html and two dozens others.

I have a windows 7 running this thing + wamp 2.5 which runs php 5.5.12

How to&Answers:

It’s nothing to do with your code, it’s a “feature” of MS Windows to make you buy the server edition (or upgrade to a different OS). Functionally there’s no difference between the server and desktop editions of the NT kernel (some different optimization tweaks) its just a means of ensuring you are complying with the terms of the licence.