Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

php - Too many FTP connections, can't accept more

I got the following error message:

Warning: ftp_login(): I can't accept more than 6 connections as the same user in C:xampphtdocsestftp_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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...