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

This topic is 3744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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#]

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

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

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

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

try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();

// Read from the file stream 2kb at a time

// 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);
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)
{
}


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.