PORT Command Usage in FTP and the Compact Framework

From: Paul [Paradise Solutions] (Paul_at_Don'tChuffingSpamMe.com)
Date: 01/05/05


Date: Wed, 05 Jan 2005 10:54:24 +0000

Hi all

I recently managed to find a proven piece of code from these groups, for
using FTP on the Compact Framework. It was originally written for the
desktop but a respected name (can't remember who) said that he had got
it working on PDA by commenting out a line.
I tried it and at was great - connected to ftp site almost first time.
I now have a prblem though. The code does work 100% on the desktop, but
the PORT command always fails on the PDA. I was wondering if anyone
could shed any light on this. I currently have the PDA 'sunk' with the
desktop via a cradle and I'm attempting to connect to a an FTP server on
me local machine. Could this setup prevent the PORT command from
succeding, or are there a restricted number or ports available to use on
the PDA and my port number is too high?
I have lokoed into IPworks, but the cost is too high as their component
does 101 things.. but I only need one bit of it, so it's not cost
effective for us.

The code for setting the port is as follows (not originally written by
me, I just put it in it's own proc and added a couple of bits):

                /// <summary>
                /// Function to find usable IPaddress and port number for FTP return path
                /// </summary>
                /// <returns>
                /// Arraylist[0]=Ipaddress object
                /// Arraylist[1]=port number as integer
                /// </returns>
                public ArrayList SetPort()
                {
                        string sOut = null;
                        string returnString=null;
                        bool bIPFound = false;
                        ArrayList returnValue=new ArrayList();

                        // get a list of IP addresses for this machine
                        IPHostEntry ipThis = Dns.GetHostByName(Dns.GetHostName());
                        Random r = new Random();
                        // we will try all IP addresses assigned to this machine
                        // the first one that the remote machine likes will be chosen
                        while (!bIPFound)
                        {
                                for(int i=0;i<ipThis.AddressList.Length;i++)
                                {
                                        string ip = ipThis.AddressList[i].ToString().Replace(".",",");
                                        int p1 = r.Next(20);
                                        int p2 = r.Next(20);
                                        p_Port = 256*p1+p2;
                                        string sPortCom = "PORT " + ip + "," + p1.ToString() + "," +
                                                p2.ToString();
                                        // Port command now looks like PORT 61,45,6,34,xx,yy where
                                        // first 4 values is your IP address and xx and yy are random numbers
                                        // 256*xx+yy will be the port number where the remote machine will
connect
                                        // and send data
                                        WriteToStream(sPortCom);
                                        sOut = m_comRead.ReadLine();

                                        RaiseInfoEvent(sOut);

                                        returnString = string.Concat(sOut + " - " + sPortCom +
"\r\n",returnString);
                                        if (sOut.Substring(0,3) == "200")
                                        {
                                                // PORT command accepted
                                                bIPFound = true;
                                                returnValue.Add(ipThis.AddressList[i]);
                                                returnValue.Add(p_Port);
                                                break;
                                        }
                                }
                        }
                        
                        return returnValue;

                }

The 'While...' is there because on the desktop I found that sometime the
command failed, but retrying succeded.

Appologies for any line wrapping.. I was sifting through it trying to
ensure no wraps, until I learnt that C# can be multi-line (I'm VB).

Any input would be useful.

Many thanks

Paul



Relevant Pages

  • Re: Mainframe FTP
    ... Subject: Mainframe FTP ... PORT 161,221,112,120,8,158 ... 200 PORT command successful. ... The client tells the server what port it will use to initiate the ...
    (bit.listserv.ibm-main)
  • Re: Mainframe FTP
    ... Subject: Mainframe FTP ... PORT 161,221,112,120,8,158 ... 331 Password required for mainframe. ... 200 PORT command successful. ...
    (bit.listserv.ibm-main)
  • Re: Active FTP transfer behind firewall
    ... > My FTP client application is issuing a port command to open a socket on ... > port 1136 on my machine to receive a file from an ftp server. ... Perhaps your firewall implements FTP stateful inspection, and opens ...
    (comp.security.firewalls)
  • IPchains Troubles
    ... I keep getting "port errors" & "no port specified" when I try to FTP see ... 503 No PORT command issued first. ... Firewall has 3 interfaces....eth0eth1 ...
    (comp.security.unix)
  • RE: Telnet/ftp problems SBS2000
    ... Please make sure your client computers are configured as both Firewall ... will find two options "Enable folder view for FTP sites" and "Use Passive ... that the control connection has been successfully established, ... (other than port 21) ...
    (microsoft.public.windows.server.sbs)