簡體   English   中英

PHP TCP套接字連接限制-Windows Server

[英]PHP TCP socket connection Limit - Windows server

我有一個奇怪的問題,我似乎找不到解決方案或更接近我遇到的問題的任何方法,

這是東西,我有一個命令行腳本通過php在命令行上運行,它接受連接並從移動應用程序客戶端讀取json格式的數據,並在json中發送適當的響應。

一切正常,除非連接數不超過256個連接。

我想知道為什么會這樣,怎么解決呢? 我已經走了這么多天,但沒有運氣!

這是腳本片段

<?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");      
    } 
}

PS:我所做的是,對該主題進行了廣泛的搜索,然后瀏覽了諸如此類的文章: http://smallvoid.com/article/winnt-tcpip-max-limit.html和另外幾十個。

我有一個運行此東西的Windows 7 +運行PHP 5.5.12的Wamp 2.5

這與您的代碼無關,它是MS Windows的“功能”,可讓您購買服務器版本(或升級到其他操作系統)。 在功能上,NT內核的服務器版本與桌面版本之間沒有區別(某些不同的優化調整),這只是確保您遵守許可條款的一種方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM