PHP TCP套接字连接限制- Windows服务器

8
我有一个奇怪的问题,我似乎找不到解决办法或任何更接近我遇到的问题的东西。
事实是,我正在通过php在命令行上运行一个scoket脚本,它接受连接并从移动应用程序客户端以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");      
    } 
}

P.S.: 我所做的是对该主题进行广泛搜索,并阅读了像http://smallvoid.com/article/winnt-tcpip-max-limit.html和其他两打文章。

我有一个运行着这个东西+ wamp 2.5的windows 7,它运行php 5.5.12。


这是命令提示符的截图,http://prntscr.com/9ul9yh。 - Ronak K
很遗憾您没有跟随您引用文章底部的链接 - http://smallvoid.com/article/winnt-network-connection-limit.html - symcbean
伙计,我确实尝试过添加所提到的D单词,但没有用!我正在使用Windows 8作为服务器,你能帮忙吗? - Ronak K
请使用https://msdn.microsoft.com/en-us/library/ee377058%28v=bts.10%29.aspx进行相关的编程操作。 - Ronak K
这里有一篇不错的文章可以看看: http://smallvoid.com/article/winnt-tcpip-max-limit.html。 - Darin Peterson
1个回答

0

这与你的代码无关,这是微软Windows的“特性”,旨在让你购买服务器版(或升级到不同的操作系统)。从功能上讲,NT内核的服务器版和桌面版之间没有区别(一些不同的优化调整),只是确保你遵守许可证条款的手段。


我曾经在Azure的Windows服务器上尝试过完全相同的事情,结果也是一样的!我应该寻找什么样的服务器规格或者其他什么信息呢? - Ronak K
1
@DragonWarrior 推荐使用 Linux ;) 如果你没有打算运行 IIS 的话,Windows 对于服务器来说有点无用。 - RandomSeed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接