Chapter 55. Transferring Files with FTP

Graham Barr

This article shows how to create an FTP (File Transfer Protocol) client. You’ve probably used ftp, a program that is a user interface to the FTP protocol. The difference between the two is subtle but important, because the program I’ll develop in this article is also an interface to FTP.

It might not surprise you to hear that most of the work has already been done: a module, Net::FTP, interprets the FTP protocol for you. So you don’t have to mess with the nuts and bolts of FTP (defined in RFC 959) to have your program send and receive files all by itself.

FTP is a client-server protocol. That is, a server listens for client connections on an agreed-upon port address (FTP uses 21 by default). Once a connection is made, the server allocates a new port for communication with the client. This leaves port 21 free to accept the connection from the next client. The client and server communicate conversationally, with the client sending commands defined in the FTP protocol to the server, and the server sending responses back to the client. This is the architecture for many well known protocols on the Internet such as SMTP, NNTP, and HTTP.

Here’s an example of a conversation between an FTP server and a client. It shows what communication is necessary to connect, log in, change directory, and retrieve a file. The commands sent from the client to the server are shown in bold.

	220 ftphost FTP server (SunOS 4.1) ready.

	> USER anonymous 331 Guest ...

Get Computer Science & Perl Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.