Link to home
Start Free TrialLog in
Avatar of casit
casitFlag for United States of America

asked on

FTPwebresponse protocol violation error.

I have the following function which worked fine.  Basically its an ftp  upload function for a C# GUI to upload mp3s to a server.  However it throws a weird error now when I try to connect to ftp.
"The underlying connection was closed: The server committed a protocol violation."

Here is the code.  Anybody have any idea on what is wrong?
FTP login info is
host=mydgbc.org
username=test@desertgatewaybaptist.org
password=testtest

and here is my c# code
[code]
private void Upload(string filename)
        {
            FileInfo fileInf = new FileInfo(filename);
            string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
            FtpWebRequest reqFTP;

            // Create FtpWebRequest object from the Uri provided
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
            // Provide the WebPermission Credintials
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

            // By default KeepAlive is true, where the control connection is not closed
            // after a command is executed.
            reqFTP.KeepAlive = false;

            // Specify the command to be executed.
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

            // Specify the data transfer type.
            reqFTP.UseBinary = true;

            // Notify the server about the size of the uploaded file
            reqFTP.ContentLength = fileInf.Length;

            // The buffer size is set to 2kb
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;

            // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
            FileStream fs = fileInf.OpenRead();
            try
            {
                // Stream to which the file to be upload is written
                Stream strm = reqFTP.GetRequestStream();

                // Read from the file stream 2kb at a time
                contentLen = fs.Read(buff, 0, buffLength);

                // Till Stream content ends
                while (contentLen != 0)
                {
                    // Write Content from the file stream to the FTP Upload Stream
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }

                // Close the file stream and the Request Stream
                strm.Close();
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                //MessageBox.Show(response.BannerMessage);
                MessageBox.Show(response.StatusDescription);
                fs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Upload Error");
            }
        }
[/code]
Avatar of casit
casit
Flag of United States of America image

ASKER

Anybody?
Hi

Without any problem it worked for me, i dint change ur code except the file name
ASKER CERTIFIED SOLUTION
Avatar of udhayakumard
udhayakumard
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And sorry, for testing i have uploaded my files to ur site named "winzip.log", u can check that using the following link:

ftp://test@desertgatewaybaptist.org@mydgbc.org/
Avatar of casit

ASKER

Yeah I was an idiot.  I realized that the folder had been renamed.  After I renamed it back to the old name it works fine.  I will split the points since it was my fault and I solved the problem.