FTP Client using the Winsock Gateway


By   Jim Guerber  on January 30, 2006 : Uploaded files: 1

Using the Winsock gateway for FTP

Making an FTP client using the Winsock Gateway presents several challenges.

TCP/IP is in the form of a stream of characters. FTP is based on a series of text lines delimited by carriage return / line feed characters. The Comet Gateway has poor buffering capabilities. This coupled with the string variable length limit of 254 characters, and the gateway's 1024 byte limit, make for some challenging programming.

The response to each command sent to the FTP server may result in a single line response or a multi-line response. For example, on the O'Reilly ftp site, the response to "USER anonymous" is "331 Anonymous login ok, send your complete email address as your password.". This is a single line response. The response to "PASS guest@signature.net" stars with the line "230-Welcome to the O'Reilly & Associates, Inc. FTP Archive.". The hyphen after the number 230 means that there will be multiple lines in the response and the last line will begin with "230 ". That blank after the 230 indicates that the response is finished.

If an FTP client is behind a firewall, each data transfer must be done with a "passive" connection. The way this is done is as follows:
1. The client issues a "PASSV" command.
2. The server starts listening on another (random) port and responds to the command like this: "227 Entering Passive Mode (209,58,173,9,144,246)."
3. The client must parse those numbers within parens and issue a connect on the IP address and port indicated -- "OPEN 209.58.173.9 37110" The port number in encoded in those last two numbers. The client must convert each of those numbers to hex, concatenate them and convert the result back to decimal to form the port number.
4. Once the port is opened, if the data is in binary mode, the client must determine when all of the data has been received, and act appropriately. This is not trivial given the poor buffering offered by the winsock gateway as explained above.

Believe me, we will fix the gateway problems in a future version of Comet, but until then, please use the attached program as a guide.

I have attached an IBS source to this message. Note a couple of things:

This program will work with comet 2005 and 2006 builds of 367 and above only. We had to add a new field to the status function that returns the actual number of bytes read or written in the last I/O to/from the gateway.

Lines 25 to 50 represent a collection of 4 sites I have used for testing this program. Cut and paste the appropriate lines to the end of the area to select that particular site.

There is a running log of all activity. This log is sent to "LPH" my standard HTML printer. Change this if you must on line 51. I use bold text to represent commands sent from the program, and status info. Normal text is the response from the FTP server or the Gateway itself.

I hope you enjoy looking at the program. I had a lot of fun writing it.

Jim




1 file was included in this post:
FTP.IBS


Click here to return to the Support Forum.     Click here to return to the Code Library.