( FTP basics through to a client ftp ".
"application tutorial by bkenwright@xbdev.net)

"; ?>
include($basedir . "templates/page_top.php"); ?>

Well I thought I should write a small tutorial on how to use winsock with the
ftp protocols....to some people the code might be a bit simple and has no error
checking...but the tutorial is an introduction to how to create your very own
Client FTP program.

If you grasp the basics from this tutorial, it shouldn't take you long to put
together a fully functional ftp program such as CuteFTP or FlashFXP etc.

Also, I'm going to introduce the basics of the WinSock again...for example
what each Winsock function call does etc...well where I think its important.

FTP...what does it mean? Well FTP is an acronym for File Transfer
Protocol, and its used to transfer files over the internet using TCP/IP.
The basic model of an FTP is that of a

client and a server.....we make a connection with the server, usually on port
21, then we can request a list of files, or upload a file...then we can close
the connection.

Note: Nearly 99% of ftp servers use port 21.

Before any coding is even going to start, I've got to show you a list of the
ftp command verbs:

Verb

Desription

CWD

Change the current directory on the server.

PWD

Print the current directory on the server.

CDUP

Moves up to the parent directory.

LIST

List the contents of a directory.

MKD

Creates a directory on the server.

RMD

Removes a directory on the server.

DELE

Removes a file from the server.

USER

Sends the username for the login.

PASS

Sends the password for the login.

ABOR

Abort the transfer.

QUIT

Closes the connection with the server.

STAT

Gets teh current status of the server.

TYPE

Toggles the binary flag on the server (TYPE A - Ascii, TYPE
I Binary).

PORT

Asks the server to connect the client.

PASV

Requests a data connection on a new port.

RETR

Requests the server to send a file.

STOR

Sends a file from the client to the server.

APPE

Same as STOR, except data is appended.

REST

Start download at a certain point.

SYST

Get the OS information of the server.

HELP

Get help on a verb.

NOOP

No operation.

Now onto the basics.....how does all this tie together? Let me go
through what we send and what we'll get back...so you'll know what to expect
when we start coding. First things first, we'll get our ftp address, (e.g.
ftp.coolserver.com) ...then we'll connect to the server and listen!

For this tutorial, I just looked around on the web and found any public ftp
server - ftp is: 'ajpo.sei.cmu.edu', you can try with any ftp server.

Well there you go! We connect to port 21 on 'ajpo.sei.cmu.edu' and the
server sends us a hello message! The number at the start of each line, is
called the completion code (220 in this case), and
indicates the type of message. The server greeting has one of two reply
completion codes - either 220 for success, or 421 if the server rejects us.

Now in reply to this greeting message we must send something back! So
what we send our username, in this case I'm just sending the word anonymous, as
where going to log on as an anonymous.

USER anonymous

And in reply we get:

331 Guest login ok, send your complete e-mail address as
password.

Okay okay, now you can see how this is going.... we send data and we get
data...usually in ascii form......but before I start putting some code examples
together..let me show a whole ftp client - server chat:

My god! What was all that above your saying...well dont' get
worried...you will learn in a sec what the above means.

We connect to the server, we get the hello message, then we send the message
'USER anonymous/r/n', and in return the server sends us a message saying to send
our password...and so we send 'PASS anonymous @ .com'.

Usually the default transfer method is using ASCII, but just to make sure, I
send the command 'TYPE A', and in response we get the return message '200 Type
set to A', which means that data is transfered using 'Ascii' (e.g. text), as
each line ending is with a CR/LF.

Next we want to do somthing a little more complex, we send the verb - 'PASV'
which returns us '227 Entering Passive Mode (212,78,206,140,140,129)', this is
an ip and a port address which will be used to transfer the data...the first 4
numbers are the ip address, and the second 2 are a port number, you work out the
port number by multiplying the first number by 256 and add it to the second.

So in this example the ip and port would be:

ip: 212.78.206.140

port = 256*140 + 129 = 35969

All we have to do then is send which file we want (e.g. myfile.txt) and start
listening on the other port, then the file will be send to it...we just buffer
it and save it to a file.

CWD ./otherfolder

250 CWD command successful

TYPE I

200 Type set to I

How about a bit of coding....get a taste for how to make our own client ftp
program...

It all begins.......

// This line is a compiler directive that includes the windows library

// settings" menu and under the "Link" tab add the necessary librarys
instead.

#pragmacomment(lib,
"wsock32.lib")

#include
<stdio.h>

#include
<windows.h>

#include
<winsock.h>

WSADATA
ws;

char
buf[10000];

void
output(char *str)

{

FILE *fp = fopen("output.txt", "a+");

fprintf(fp, "%s\n", str);

fclose(fp);

}

int_stdcall WinMain(HINSTANCE, HINSTANCE,
char* k, int
l)

{

int d = WSAStartup(0x101, &ws);

sprintf(buf, "WSASTARTUP = %d", d);

output(buf);

return 0;

}

Output:

WSASTARTUP = 0

Well its not much to look at, but this is are starting stone! Here
above, we've created an output function so that we can write out any information
to it, so we can look at it....I've also included the WinSock startup up funtion
WSAStarup, which returns 0 if all went okay. Don't forget to include the
wsock32.lib, else you'll get linking errors.

Well a lot of the lines I've outputted to the file are to show you what's
actually happening inside, and it allows you to see if any errors have popped up
while you where entering the code.

Notice the last line, which is the reply from the ftp server on port 21, it
says '220-ada.pair.com NcFTPd Server (licensed copy) ready.', and as I mentioned
before, the 220 indicates that the server is willing and ready.

Also remember that recv(..) is a blocking function...so when you call it, it
wont' return or let you carry on with the code until it has received
something...you'll find in windows that you can use asynchronous sockets which
won't block, they usually send your application a message when data has been
received.

Now for our next trick, lets have a try at talking to our ftp server....as
you'll probably see in a minute, most of the hard work is done now..its just a
matter of sending the correct verbs, and checking the received values.

Tadaaaa....yup, we've successfully logged into an ftp server, and
authenticated ourselves. Yup, I know in the beginning it was looking
rather tricky, but its really simple once you get the basics down. I'll
tidy up the code a bit, and demonstrate how to get and send a file, and how to
get information from the ftp server.... now maybe you might want to get a large
mug of coffee about now...its not easy having to learn all this stuff in one big
go...

Hold on... where going deeper!

Here is the code, now its exactly the same code as above, but I've put it
into tidy parts....it will output the exact same as the above output, but its
been organised into functions....a connection function, a send function, and a
receive funtion. As the only paramater that is important is the data which
is send (e.g. USER anonymous) and the SOCKET number...SOCKET if you look deep in
the .h files is just an unsigned integer...so we pass that to our recv and send
functions...here is the code:

Now that I've tidied up the code, let me send and record more of the FTP verb
commands, and see what we get.....a lot of the code will be repeated, e.g. you
will get familiar with certain functions.....but I think its good to be able to
see the fully working program....the more times you go over it the more familiar
you'll become with it. So once your sure you understand how the above code
works...have a go at sending and receiving your own 'verbs'...e.g. send the
command to the server "HELP" and see what it sends back.

Well this code does more or less the same as above, but I've called some more
of the commands on the server so show you some typical responses.

Well this is sure turning into a long tutorial, now for the next part....I've
goto show you how to download a file from the server! Its not going to be
easy, but I'm sure you'll be able to manage....take my hand and follow me step
by step through it...

I've commented the new lines of code in this example with
//NEW
NEW NEW..etc so you can see which are the lines that I've added to
get the IP and port address from the returned string from the server.

Well your going to be thinking at this point...so what do we do with this new
IP and port number....well we us this new IP and port number to either send or
receive the file where interested in from or to the server. We simply tell
the server what where going to do, either download or send a file, then we get
the okay message....we connect to this new IP and port, then send or receive it.

I''ve wrote the next piece of code....which is more or less the same as
above, but with the added code to get a file from the server.

I'm sure your getting the hang of it by now... and I've
started you off on the road to creating your own ftp client...or maybe even a
ftp server. To finish the tutorial I'll show you a text list of a number
of commands sent and received from an ftp server.... I've added comments which
are between the /* comment */ markings....so I can outline things within it.

Feedback: As usual, with any tutorials, feedback is always
appreciated...again this tutorial might be a little easy for some, or maybe a
little complex for those unfamiliar with C and C++, but if you come across any
errors or maybe something which you think would be invaluable, please feel free
to email me any comments anytime.... Well until next time...