Return to homepage
   
 
Jump to Article
   
 

Building an FTP client on your Pocket PC

Introduction

The File Transfer Protocol (FTP) is a standard that has been around for many years. It was last updated back in 1985 and is still one of the most widely used file transfer protocols used over the Internet.

Since most devices now are Internet ready, it makes sense to utilise the FTP standard to send and receive files to and from your device (client) and the server. Whether you have commercially hosted web space or an FTP server running within your own network, allowing your Pocket PC device to communicate with it is a very powerful tool.

You can now send to your server documents, data files, even digital images. The possibilities are whatever you can imagine. Alternatively, you may be downloading the latest data from the server, possibly product images, route map directions, etc.

You would think that a well established standard such as FTP would make it simple to develop an FTP client in eVB or VB.NET. In the past, it hasn’t always been that simple. Experienced eVB developers know that they can use Application Programming Interface (API) calls to the underlying core function in the Pocket PC device to have access to FTP.

Unfortunately, due to a few bugs in the Pocket PC operating system, it isn’t that simple. But, there is good news because Sapphire Solutions (http://www.sapphire-solutions.co.uk) came up with an FTP component that is simple to use and overcomes these underlying bugs.

The Sapphire FTP Utility does exactly what it says in the title. It allows you to connect to an FTP server and perform the usual FTP functions, whether it is directory searching, creating directories, uploading/downloading files, deleting files, etc.

Utility overview

The Sapphire FTP Utility allows you to connect to a remote FTP server. You can perform directory searches, upload and download files, manage directories and files, etc. It also supports Passive and Active mode transfers (see note below).

Feature list

The Sapphire FTP Utility supports the following features:

  • Open a session
  • Close a session
  • Upload and Download files
  • Transfer files in 1K chunks
  • Change the remote directory
  • Retrieve the current remote directory
  • Search for file and directories with attributes
  • Remove files and directories
  • Rename files and directories
  • Make directories
  • Upload and download directories and contents
  • Check if a remote file exists

Running the demo project

Sapphire have also provided an extensive demo project in both eMbedded Visual Basic and VB.NET that show how to use all the functions of the DLL. You get all the eVB and VB.NET source code which is not only great for experienced programmers to implement the utility, but also great for new programmers to see how the application has been approached.

The latest version of the Sapphire FTP Utility can be downloaded from their web site here:
http://www.sapphire-solutions.co.uk/product.asp?product=FTP

Once you have downloaded the demo project and unzip it, you will have two versions of the demo project, one for eVB and the other for VB.NET. You will also have a folder containing the different DLLs compiled for the different devices.

Simply select the DLL that matches the device. If you are developing in eVB, then you will need to copy the DLL to the \Windows\ path. Alternatively, if you are developing in VB.NET or C#, you need to copy the DLL to the application path. Check the project settings to see where the demo project will deploy itself to.

You will also get a DLL that will run on standard Windows PCs so you can build an FTP compatible application with the same ease of use.

Now you are ready to run the demo. Please note the following screenshots have been taken from the VB.NET demo project.

FTP Demo Menu

FTP Demo Menu

FTP server configuration

FTP Settings

FTP Settings

Transferring files

FTP Explorer

FTP Explorer

FTP Explorer - Functions Menu

FTP Explorer - Functions Menu

A note about passive and active mode transfers

So how do you know when to use active or passive mode. Well, before we go into that, let me explain a little about how FTP works.

When you make an FTP connection to your server you create what is called a control connection. This is most commonly done using port 21. This control connection allows the client to request actions from the server. This could be as simple as logging into the server to creating directories and deleting files.

There are two types of data that you will transfer. The first will be the directory listing and the second will be a file. The control connection will request that a data connection be opened for the actual data. The data connection can either be used to send data (upload a file) or receive data (download a file or directory listing).

It is then common for an FTP connection to have a new connection created to your server for this so called data connection. So, who connects to whom for the data connection? Does the client connect to the server again (just like for the control connection on port 21)? Or does the server connect back to the client?

This is where active and passive mode comes in.

Active Mode

This is where the server connects back to the client. This will only work if the server can see the IP address of the client. If the client’s IP address is routed through an internal network or behind a firewall, then the server will not be able to see it and the data connection will timeout.

Passive Mode

This is where the client creates the connection to the server. This is ideal if your client is behind a firewall. But sometimes the FTP server may be behind a firewall and won’t allow any other connections other than port 21. You would have to contact the administrator of the server to allow some passive connection port numbers to be allowed by the firewall if active mode isn’t an option.

In Summary

  • If your client is behind a firewall or router, connect in passive mode.
  • If your server is behind a firewall then connect in active mode.
  • If none are behind a firewall/router then either will be fine.
  • If both are behind a firewall/router, get help from the server’s administrator.

Sample code

Connecting to the FTP server

Dim lResult As Integer

Dim lPassive As Integer

Dim lTimeOut As Integer

 

 

If MsgBox("Connect in Passive mode?", vbYesNo, "Sapphire FTP Explorer") = vbYes Then

  lPassive = 1

Else

  lPassive = 0

End If

 

lTimeOut = 10000 'Timeout in milliseconds

lResult = sFTPOpen(“ftp.server.com”, 21, “user”, “password”, lPassive, lTimeOut, SapphireFTPLicence)

If lResult <> FTP_NO_ERROR Then

  MsgBox("Unable to connect to server" & vbNewLine & vbNewLine & "Error: " & _

        GetFTPError(lResult), vbOKOnly + vbExclamation, "Sapphire FTP")

Else

  MsgBox("Connect to server", vbOkOnly, "Sapphire FTP")

End If

Disconnecting from the server

Dim lResult As Integer

lResult = sFTPClose(SapphireFTPLicence)

 

If lResult <> FTP_NO_ERROR Then

  MsgBox("Unable to close connection" & vbNewLine & vbNewLine & "Error: " & _

        GetFTPError(lResult), vbOKOnly + vbExclamation, "Sapphire FTP")

Else

  MsgBox("Disconnected", vbOkOnly, "Sapphire FTP")

End If

Uploading a file

lBytes = 0

lResult = sFTPPushStart("\My Documents\File.zip", "RemoteFile.zip", FTP_TRANSFER_BINARY, SapphireFTPLicence)

If lResult <> FTP_NO_ERROR Then

  MsgBox("Unable to upload file" & vbNewLine & vbNewLine & "Error: " & _

        GetFTPError(lResult), vbOKOnly + vbExclamation, "Sapphire FTP")

Else

  Do

    lResult = sFTPPushMore()

    lBytes = lBytes + 1

    If (lBytes Mod 10 = 0) Then

      lblProgress.Text = "Uploaded " & lBytes & "K of File.zip"

      lblProgress.Update()

    End If

  Loop While (lResult = 0)

  If lResult <> 100 Then

    MsgBox("File upload not completed" & vbNewLine & vbNewLine & "Error: " & _

          GetFTPError(lResult), vbOKOnly + vbExclamation, "Sapphire FTP")

  End If

 

  lResult = sFTPPushEnd()

  If lResult <> FTP_NO_ERROR Then

    MsgBox("Unable to close files" & vbNewLine & vbNewLine & "Error: " & _

          GetFTPError(lResult), vbOKOnly + vbExclamation, "Sapphire FTP")

  End If
End If

Manual documentation

Sapphire Solutions also provide a comprehensive manual for all their utilities. It includes a definition of the DLL function calls, some sample code and FAQs to help with any troubleshooting.

Their manual documentation can be viewed online, downloaded as a HTML document for offline use and even a downloadable Windows help file. These can all be found on their web site at http://www.sapphire-solutions.co.uk/manual.

Licence of use

Sapphire Solutions provide an unlimited use demo version of the Sapphire FTP Utility. While the product will function as normal, the user will receive a popup message stating that it is a demo version. It is great for trying out the utility, checking device compatibility, building it into your application and selling the idea to your customer. Students can use it as well for their college or university projects without having to purchase it either.

Of course though, for commercial applications being developed you will need to get a licence for the utility to make it run without any pop-ups. The good thing about Sapphire’s pricing policy is that it is a one off fee and you or your company can use it as many times as you like for your projects royalty free. There are no royalties required for rolling out your application to your client.

With that you will also get 12 months support via email to their experienced technical team. They also provide upgrades for the latest versions at discounted prices which also extend the 12 months support by a further 12 months.