You are on page 1of 6

Birla Institute of Technology & Science, Pilani, K. K.

BIRLA Goa campus


Computer Networks
Second Semester 2014-2015
Lab 9
FTP Socket Programming
--------------------------------------------------------------------------------------------------------------------1.1 Introductory material:
File Transfer Protocol (FTP) is one of the oldest applications in use on the Internet. The File
Transfer Protocol (FTP) is used to transfer files between two computers over a network and
Internet. In this manual we will look how to implement FTP communication.
1.2 What is FTP?
When you want to copy files between two computers that are on the same local network, often
you can simply "share" a drive or folder, and copy the files the same way you would copy files
from one place to another on your own PC.
What if you want to copy files from one computer to another that is halfway around the
world? You would probably use your Internet connection. However, for security reasons, it is
very uncommon to share folders over the Internet. File transfers over the Internet use special
techniques, of which one of the oldest and most widely-used is FTP. FTP, short for "File
Transfer Protocol," can transfer files between any computers that have an Internet connection,
and also works between computers using totally different operating systems.
Transferring files from a client computer to a server computer is called "uploading" and
transferring from a server to a client is "downloading".

1.3 How FTP works?


FTP uses one connection for commands and the other for sending and receiving data. FTP
has a standard port number on which the FTP server "listens" for connections. A port is a
"logical connection point" for communicating using the Internet Protocol (IP). The standard
port number used by FTP servers is 21 and is used only for sending commands. Since port

21 is used exclusively for sending commands, this port is referred to as a command


port. For example, to get a list of folders and files present on the FTP server, the FTP Client
issues a "LIST" command. The FTP server then sends a list of all folders and files back to
the FTP Client. So what about the internet connection used to send and receive data? The
port that is used for transferring data is referred to as a data port. The port number for data
port is 20.
1. The client FTP application opens a control connection to the server on destination port
21, and specifies a source port as the source to which the FTP server should respond
(using TCP). The control connection is mainly used to send ftp commands.
2. The server responds over the control connection with three-digit status codes in ASCII
with an optional text message. For example "200" (or "200 OK") means that the last
command was successful.
3. The FTP server opens a second connection for data on port 20 to the original client.
4. The client responds on the data port, completing a TCP connection.
5. Data transfer begins.
6. The server indicates the end of the data transfer
7. Client closes the connection once the data is received.
8. The data connection is closed.
9. The FTP connection is closed.
1.4 Common FTP Commands
?

to request help or information about the FTP commands

ascii

to set the mode of file transfer to ASCII


(this is the default and transmits seven bits per character)

to set the mode of file transfer to binary


binary (the binary mode transmits all eight bits per byte and thus provides less chance of a
transmission error and must be used to transmit files other than ASCII files)
bye

to exit the FTP environment (same as quit)

cd

to change directory on the remote machine

close

to terminate a connection with another computer

delete to delete (remove) a file in the current remote directory (same as rm in UNIX)
get

to copy one file from the remote machine to the local machine

help

to request a list of all available FTP commands

lcd

to change directory on your local machine (same as UNIX cd)

ls

to list the names of the files in the current remote directory

mkdir to make a new directory within the current remote directory


mget

to copy multiple files from the remote machine to the local machine;
you are prompted for a y/n answer before transferring each file

mput

to copy multiple files from the local machine to the remote machine;
you are prompted for a y/n answer before transferring each file

open

to open a connection with another computer

put

to copy one file from the local machine to the remote machine

pwd

to find out the pathname of the current directory on the remote machine

quit

to exit the FTP environment (same as bye)

rmdir to remove (delete) a directory in the current remote directory

1.5 FTP reply codes:


The first digit is used to indicate one of three possible outcomes success, failure, or to
indicate an error or incomplete reply:

2yz Success reply


4yz or 5yz Failure reply
1yz or 3yz Error or Incomplete reply

The second digit defines the kind of error:

x0z Syntax. These replies refer to syntax errors.


x1z Information. Replies to requests for information.
x2z Connections. Replies referring to the control and data connections.
x3z Authentication and accounting. Replies for the login process and accounting
procedures.
x4z Not defined.
x5z File system. These replies relay status codes from the server file system.

The third digit of the reply code is used to provide additional detail for each of the categories
defined by the second digit.
1.6 Exercise
Write a program to implement a simple FTP server and client which allows the following
operation:
a) List the files in the server (eg: ls)
b) get a file from the server (get <filename>)
c) put a file to the server (put <filename>)
Note:
Client and server programs should be run in different directories.
References:
1. http://www.deskshare.com/resources/articles/ftp-how-to.aspx
2. http://www.cs.colostate.edu/helpdocs/ftp.html

Pseudocode for FTP client.c


sd=socket(AF_INET, SOCK_STREAM, 0)
get server host name from argv[1]
connect(sd, &server-socket-address,...)
While (1) {
fgets a ftp command line from keyboard
if the command is "put a existed file"
1. send the command to the server
2. open the file
3. read the file
4. write the file to server
5. close the file
if the command is "put a non-existed file"
display "filename: no such file on client"
if the command is "get a file"

1. send the command to the server


2. fgets first line from the server:existed or nonexisted
3. if existed
3.1 read the file from the server
3.2 write the file to the local directory
4. if nonexisted
display "filename: no such file on server"
if the command is "cd ..." , "ls ...", or pwd
1. send the command to the server
2. fgets a reply line from the socket to see if the command is successfully executed
3. read from the socket and display correspondingly.
if the command is "quit"
1. close the socket
2. break or exit
otherwise: show "An invalid ftp command."
}
Pseudocode for FTP server.c
While (1) {
fgets a ftp command line from client via stream socket
if the command is "put a file"
1. create a file
2. read the file from socket
3. write to the file
4. close the file
if the command is "get a file"

1. open the file


2. if existed
2.1 send "existed" to client
2.2 read the file
2.3 write the file to client
2.4 close the file
3. if nonexitested
send "nonexisted" to client
if the command is "ls ..." or "pwd"
1. fp =popen(command,"r")
2. read the result from fp
3. if no result from fp, reply "wrong command usage!" to client, otherwise reply
"successfully executed!" to client.
4. send the result to client
if the command is "quit"
1. close socket
2. exit
}
P.S.: This is only suggested pseudo code format. You can follow your own style of
programming.

You might also like