FTP 连接太多,无法接受更多

2022-01-09 00:00:00 server ftp php ftp-client

我收到以下错误消息:

<块引用>

警告: ftp_login():我在第 58 行的 C:xampphtdocs estftp_sync.php 中不能以同一用户接受超过 6 个连接

导致错误的代码:

函数 newStream($i){$conId = ftp_connect($this->ftpServer);//使用用户名和密码登录$login_result = ftp_login($conId, $this->ftpUsername, $this->ftpPassword);//第58行///home/content/61/10367861/html///开启被动模式ftp_pasv($conId, true);$this->conIds[$i]=$conId;$this->localFiles[$i]='';$this->conStats[$i]=FTP_FAILED;//初始值}

有没有人知道这个错误信息是什么意思?

解决方案

抛出此错误是因为您的服务器限制了每个用户/IP 地址的最大连接数.大多数人遇到的错误大多是这样的:

<块引用>

421我不能接受超过 [0-9]+ 个连接作为同一用户

<块引用>

421 来自此 IP 的连接过多 ([0-9]+)

这种错误最常使用的FTP错误代码是421.在 RFC 959 (FTP) 中定义为:

<块引用>

421 服务不可用,正在关闭控制连接.这可能是一个如果服务知道它必须关闭,则回复任何命令.

您现在有两种可能的解决方案来解决这个问题:

  1. 减少程序建立的连接数量.
  2. 增加与您的服务器的允许 FTP 连接数量.

如果允许的最大值低于 3,那么您应该首先尝试更改服务器上的配置文件,因为大多数程序至少需要 3,有时需要 2.

  1. 减少连接数量


在程序中遇到问题时:一些 FTP 客户端允许用户在设置中更改使用的连接数量.他们大多同时使用大约 3 个连接,2 个用于提高性能,1 个用于在用户执行其他任务时启用浏览.如果您在执行其他任务时不担心浏览,您可以将数量减少到 2 甚至 1,而不会丢失任何重要功能.(FileZilla 例如允许这样做.)

在您自己的代码中遇到问题时:减少并发执行的任务数量.还要检查您的代码是否以正确的方式关闭连接,以及在引发错误时.无论发生什么,它都应该始终关闭.在 PHP 中,您可以在一个类中使用 try-catch-blocks将关闭连接的代码放入__destruct 方法中.p>

  1. 增加允许的连接数


这取决于您使用的 FTP 服务器类型.在 PureFTP (大多数 UNIX 系统使用)中,您需要更改 /etc/pure-ftpd.conf 中的 MaxClientsPerIP 设置.默认配置文件或托管公司的大多数管理员设置的默认数量约为 5-15.增加值,直到它适合您的需要.请注意,理论上介于最多用户和 FTP 服务器之间的代理服务器可能会导致问题,因为大多数连接将使用相同的 IP 地址.


在您的特殊情况下:正如 Mave 所述,您并没有关闭连接在你的代码中.这很容易导致多个连接处于活动状态,特别是如果您在短时间内多次运行代码.因此,在您的特定情况下,添加 ftp_close($conId); 可以解决问题.(也可以使用 try-catch-block.)

I got the following error message:

Warning: ftp_login(): I can't accept more than 6 connections as the same user in C:xampphtdocs estftp_sync.php on line 58

My code which causes the error:

function newStream($i){
        $conId = ftp_connect($this->ftpServer);

        // login with username and password
        $login_result = ftp_login($conId, $this->ftpUsername, $this->ftpPassword);//line 58
        // /home/content/61/10367861/html/

        // turn passive mode on
        ftp_pasv($conId, true);

        $this->conIds[$i]=$conId;
        $this->localFiles[$i]='';
        $this->conStats[$i]=FTP_FAILED;//initial value
    }

Does anyone probably know what this error message means?

解决方案

This error is thrown because your server restricts the maximum number of connections per user / IP address. The errors the most people encounter look mostly like this:

421 I can't accept more than [0-9]+ connections as the same user

421 Too many connections ([0-9]+) from this IP

The FTP error code which is mostly used for this kind of error is 421. Which is defined in RFC 959 (FTP) as:

421 Service not available, closing control connection. This may be a reply to any command if the service knows it must shut down.

You have now exactly two possible solutions to solve this problem:

  1. Reduce the amount of connections made by your program.
  2. Increase the amount of allowed FTP connections to your server.

If the allowed maximum is beneath 3, then you should first try to change the config file on your server, because the most programs need at least 3, sometimes 2.

  1. Reducing the amount of connections


When facing the problem within a program: Some FTP clients allow the user to change the amount of used connections in the settings. They are mostly using about 3 connections at the same time, 2 to increase the performance, 1 to enable browsing while the user is performing other tasks. You can decrease the amount without losing any important functionality to 2 or even to 1, if you don't bother about browsing while performing other tasks. (FileZilla for example allow this.)

When facing the problem within your own code: Reduce the amount of tasks which are performed concurrently. Check also if your code is closing the connections the right way, also when errors are thrown. It should always be closed, no matter what happens. In PHP you could use try-catch-blocks, inside a class you could put the code for closing the connection into the __destruct method.

  1. Increase the amount of allowed connections


This depends on what kind of FTP server you are using. In PureFTP (used by the most UNIX systems) you need to change the MaxClientsPerIP setting in /etc/pure-ftpd.conf. The default amount set by the default configuration file or by the most administrators at hosting companies is somewhere about 5-15. Increase the value until it fits your needs. Be aware that theoretically a proxy server which is somewhere between the most users and the FTP server could cause trouble, because the most connection will then use the same IP address.


In your special case: As Mave mentioned, you aren't closing the connections in your code. This could easily cause multiple connections being active, especially if you are running the code in a short period of time multiple times. So, in your specific case would adding ftp_close($conId); fix the problem. (Use also a try-catch-block.)

相关文章