Is it acceptable for code using FTP for file transferring to have several connections opened-closed?
Given this simple 2-step pseudocode: 1. request for a file listing 2. download each file
Where each step requires opening a new connection. For step 2, a new connection is also opened per file that needs to be downloaded. The reason why I think it was coded this way is so that any FTP exception encountered (for example 421, connection closed), the downloading of the other files will p开发者_C百科ush through (unless of course step 1 encounters the error, thus no subsequent step 2 will happen).
I'm new to FTP, so this is something that's been bugging me quite a bit.
Yes, it's perfectly normal. FTP is a two connection protocol. A command stream (get, put, dir, cd, etc...) and a data stream (the actual files being sent back and forth). The command stream is kept open for the duration of your connection (and is considered "the connection"). The data stream exists only while sending/retrieving data and will be opened/closed multiple times in a typical FTP session.
The source of the data connection depends on if you're using active of passive ftp. For active connections, the ftp server will initiate the data connection back to the client. This is done via the client saying "do a 'dir' command, and send its results back to IP x.x.x.x, port yyy". Passive works in the opposite fashion. The client initiates all connections to the server, and doing dir is basically. "Give me a directory listing" and getting back a "Ok, directory data is available at IP x.x.x.x, port YYY - come and get it".
On the protocol level, the client will send PORT x.x.x.x.y.z to notify server to open an active mode data channel connection back to client at x.x.x.x on port (y * 256)+z. The client sends PASV to request passive mode; in this case the server's response contains x.x.x.x.y.z telling client to open connection at ip and port
精彩评论