Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am using AT commands on gprs to do ftp . We have everything working until we send RETR a.exe .

The file data is also sent on the port. But it contains the response headers like RECEIVE:1,1400 ,may be from the modem of the device and ftp server responses also like 150 connection accepted etc. How do we write this file so that we will have a uncorrupted a.exe which will run on the client device. Please advise. We are using C#. Should we remove the headers. How do we do it . Get a good exe file ?

And even to remove unwanted characters from the ftp data, we need to remove single single characters from the string and its taking so much time to remove it (and all other string functions are not working here like replace or remove).

And if we are using sockets, its giving problem in opening the data connection through the socket.

(Device used --- winCE )
Thanks
Here is the code i am using
C#
void Readfully()        
        {
            byte[] chunk = new byte[4096];
            int count;
            int flg = 0;
            string temp_ReceveivedFile = "";
            string result = "";
            StreamWriter writer = new StreamWriter(File.Open("a.exe", FileMode.Create));
        back:
            try
            {
                Array.Clear(chunk, 0, chunk.Length);
                while (true)
                {
                    count = port.Read(chunk, 0, chunk.Length);
                    temp_ReceveivedFile = System.Text.Encoding.ASCII.GetString(chunk, 0, count);
                    writer.Write(temp_ReceveivedFile);
                    //writer.Flush();                    
                    if (temp_ReceveivedFile.Contains("Transfer OK"))
                    {
                        break;
                    }
                    else if (temp_ReceveivedFile.Contains(" 425 Can't open data connection."))
                    {
                        System.Windows.Forms.MessageBox.Show(" 425 Can't open data connection.");
                        break;
                    }
                    flg = 0;
                    Array.Clear(chunk, 0, count);
                }
            }
            catch (Exception ex)
            {
                flg++;
                if (flg < 3)
                    goto back;
            } 
            //System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(pattern);                           
            writer.Close();
           * /
            //..........
            StreamReader sr = new StreamReader("a.exe");
            String RWBuff;
            StreamWriter wr = new StreamWriter(File.Open("a_new.exe", FileMode.Create));
            while (true)
            {
                RWBuff = sr.ReadLine();
                if (RWBuff.Contains("+RECEIVE,1,1400:"))
                {
                    break;
                }
            }
            while(true)
            {
                try
                {
                    RWBuff = sr.ReadLine();

                    if (!RWBuff.Contains("Transfer OK"))
                    {
                        if (!RWBuff.Contains("+RECEIVE,1,1400:") && RWBuff!=null)
                        {
                            result = RWBuff + "\n";
                            wr.Write(result);
                        }
                    }
                    else
                        break;
                }
                catch(Exception Eww)
                {}
            }
            sr.Close();          
            
            wr.Close();
            //..........
        }



        }
    }

Here port.read is not reading all bytes of data
Posted
Updated 9-Aug-12 5:11am
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900