I have a problem implementing (APPE)ND functionality in this class. One of our clients requested exporting data via FTP, so I need to connect to an FTP and add rows to a textfile. I could just download the file first append new text and upload back to the server, but where's the fun in that?
The method I wrote looks like this and genrates the "Can't open data connection." exception in the createDataSocket() method:
public void AppendToFile(string localFile, string remoteFile)
{
if ( !this.loggedin ) this.Login();
this.sendCommand("APPE " + remoteFile);
if(this.resultCode == 150)
{
Socket socket = this.createDataSocket();
FileStream input = new FileStream(localFile,FileMode.Open);
while ((bytes = input.Read(buffer,0,buffer.Length)) > 0)
{
socket.Send(buffer, bytes, 0);
}
input.Close();
if (socket.Connected)
{
socket.Close();
}
}
}
I can connect, login, upload and download without any issues. In my code I call GetFileList() method before Appending to get the file to append to. But that method cannot hog the socket, right?
Maybe I've misunderstood the usage of the APPE command (even though I've read the entire FTP RFC).
I'm not that experienced in socket programming either, I just copied the code from the Upload method and removed all of the filechecks.
Did I miss something fundamental?