[.net] same code, different project, SocketException...

Started by
-1 comments, last by neonic 16 years, 4 months ago
What I am trying to do is make a BackgroundWorker based FTP command that uploads a file and updates a Progress bar while I am doing that. So I created a new project and wrote this code: that sits in the DoWork function of backgroundWorker1
[source lang=c#]
string filename = "MySQLAdministrator.exe";


            FileInfo fileInf = new FileInfo(filename);
            //string uri = "ftp://" + ftpServerIP + "/" + remotePath + "/" + fileInf.Name;
            FtpWebRequest reqFTP;

            // Create FtpWebRequest object from the Uri provided
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + "dumptruck" + "/" + filename));

            // 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


                int count = 1;
                float percentage = 0;
                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);
                    percentage = (float)buffLength * count / (float)fileInf.Length * 100;
                    //  MessageBox.Show(percentage.ToString());
                    this.backgroundWorker1.ReportProgress((int)percentage);
                    count++;
                }

                // Close the file stream and the Request Stream
                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Upload Error");
            }



Ok. So that code does exactly what I want it to do. I just call ReportProgress() in the while loop where it streams the data, and work out a percentage, then in the ProgressChanged() function I just update the value of the progressbar to match, and it works perfectly.Now the problem comes in when I *literally* cut and paste the code into the other project that I have. Same code, assume that ALL the variables are initialized with valid data, so it isn't like theres any null strings being passed or anything... And I step through the code, and it gives me this error: " A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll Additional information: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond " on this line: Stream strm = reqFTP.GetRequestStream(); It seems to mean that the server can't be reached... But it is strange because the server IP is the SAME as in the other project, and I can run that one every time... I can ping the FTP server and can connect to it in FileZilla, so I suspect something very strange is going on here. Any help you could give me would be great! Cheers.

This topic is closed to new replies.

Advertisement