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

标签 php windows sockets tcp server

我有一个奇怪的问题,我似乎找不到解决方案或任何更接近我遇到的问题的方法,

事情是这样的,我有一个通过 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和另外两打。

我有一个运行这个东西的 Windows 7 + 运行 php 5.5.12 的 wamp 2.5

最佳答案

这与您的代码无关,它是 MS Windows 的一项“功能”,让您购买服务器版本(或升级到不同的操作系统)。在功能上,NT 内核的服务器版和桌面版之间没有区别(一些不同的优化调整),它只是确保您遵守许可条款的一种方式。

关于PHP TCP套接字连接限制 - Windows服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34993128/

相关文章:

python - 使用tor代理时如何控制tor

c - 为什么 select() 函数在我的 UDP 服务器实现中总是返回 0?

php - 检查字符串是否包含PHP中的下划线

Php PDO,让用户还可以定义要选择的字段

java - 无论我做什么都无法安装 spring-boot-starter-parent

c# - 使用 windbg 调试 .NET 转储

php - 可以将 memcached 键的生存时间 (TTL) 设置为无限大吗?

使用变量的 PHP SQL 更新

python - Python 3 和 Windows 64 中的内存错误

c - C 中用于网络编程的 accept() 函数