FTP连接过多,无法接受更多 [英] Too many FTP connections, can't accept more

查看:219
本文介绍了FTP连接过多,无法接受更多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误消息:

警告::ftp_login():在第58行的C:\ xampp \ htdocs \ test \ ftp_sync.php中,我不能以同一用户的身份接受超过6个连接

我的导致错误的代码:

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
    }

有人知道这个错误消息是什么意思吗?

解决方案

由于服务器限制了每个用户/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方法.

2.增加允许的连接数量

这取决于您使用的是哪种FTP服务器.在 PureFTP (大多数UNIX系统使用)中,您需要更改MaxClientsPerIP/etc/pure-ftpd.conf中进行设置.由默认配置文件或托管公司的大多数管理员设置的默认数量大约为5-15.增加价值,直到它适合您的需求. 请注意,从理论上讲,位于大多数用户和FTP服务器之间的代理服务器可能会造成麻烦,因为大多数连接将使用相同的IP地址.


在您的特殊情况下:所述,您并没有关闭连接在您的代码中.这很容易导致多个连接处于活动状态,尤其是在短时间内多次运行代码的情况下.因此,在您的特定情况下,添加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:\xampp\htdocs\test\ftp_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.

2. 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.)

这篇关于FTP连接过多,无法接受更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆