/// <summary>Creates a data socket.</summary> /// <param name="Filename">Specifies the local filename.</param> /// <param name="Filemode">Specifies the file mode.</param> /// <param name="StreamDirection">Specifies the direction of the stream.</param> /// <param name="AppendFrom">Specifies where to append from.</param> /// <returns>Returns True if it was successfully created, False otherwise.</returns> private bool CreateDataSocket(string Filename, DataConnection.FileModes Filemode, DataConnection.StreamDirections StreamDirection, long AppendFrom) { try { dataSocket = new DataConnection(); dataSocket.Filename = Filename; dataSocket.DownloadToFile = (Filename != ""); dataSocket.AppendFrom = AppendFrom; dataSocket.FileMode = Filemode; dataSocket.StreamDirection = StreamDirection; dataSocket.DataSent += new DataConnection.DataSentHandler(DataSent); dataSocket.DataReceived += new DataConnection.DataReceivedHandler(DataReceived); if (passiveTransfers) { SendCommand("PASV"); if (lastResponseType == 2) { int BeginPos = lastResponse.IndexOf("("); int EndPos = lastResponse.IndexOf(")", BeginPos + 1); if (BeginPos > 0 && EndPos > 0) { string [] Output = lastResponse.Substring(BeginPos + 1, EndPos - BeginPos - 1).Split(','); if (Output.Length == 6) { passiveEndPoint = new IPEndPoint(IPAddress.Parse(Output[0] + "." + Output[1] + "." + Output[2] + "." + Output[3]), int.Parse(Output[4]) * 256 + int.Parse(Output[5])); dataSocket.RemoteEndPoint = passiveEndPoint; dataSocket.Connect(); } } } } else { dataSocket.Listen(); } } catch { return(false); } return(true); }
/// <summary>Uploads a file to the remote server.</summary> /// <param name="LocalFile">Specifies the local file to upload.</param> /// <param name="RemoteFile">Specifies the remote file to upload to.</param> /// <param name="FileMode">Specifies the file mode.</param> /// <param name="AppendFrom">Specifies where to resume from.</param> /// <returns>Returns True if the request was successful, False otherwise.</returns> public bool UploadFile(string LocalFile, string RemoteFile, DataConnection.FileModes FileMode, long AppendFrom) { return(UploadFile(LocalFile, RemoteFile, StreamModes.Binary, FileMode, AppendFrom)); }
/// <summary>Uploads a file to the remote server.</summary> /// <param name="LocalFile">Specifies the local file to upload.</param> /// <param name="RemoteFile">Specifies the remote file to upload to.</param> /// <param name="StreamMode">Specifies the transfer type.</param> /// <param name="FileMode">Specifies the file mode.</param> /// <param name="AppendFrom">Specifies where to resume from.</param> /// <returns>Returns True if the request was successful, False otherwise.</returns> public bool UploadFile(string LocalFile, string RemoteFile, StreamModes StreamMode, DataConnection.FileModes FileMode, long AppendFrom) { SendCommand("TYPE " + Convert.ToChar(StreamMode)); if (!CreateDataSocket(LocalFile, FileMode, DataConnection.StreamDirections.Upload, AppendFrom)) { OnCommandCompleted(); return(false); } if (!passiveTransfers) { IPEndPoint MyEndPoint = dataSocket.GetLocalEndPoint; SendCommand("PORT " + MyEndPoint.Address.ToString().Replace(".", ",") + "," + ((int)Math.Floor(MyEndPoint.Port / 256)).ToString() + "," + (MyEndPoint.Port % 256).ToString()); } if (FileMode == DataConnection.FileModes.Append && AppendFrom > 0) { SendCommand("REST " + AppendFrom.ToString()); SendCommand("APPE " + RemoteFile); } else { SendCommand("STOR " + RemoteFile); } if (lastResponseType != 4 && lastResponseType != 5) { try { dataSocket.SendToSocket(); } catch { dataSocket.Close(); WaitForResponse(); OnCommandCompleted(); return(false); } WaitForResponse(); } dataSocket.Close(); OnCommandCompleted(); return(true); }
/// <summary>Downloads a file from the remote server.</summary> /// <param name="RemoteFile">Specifies the remote file to download.</param> /// <param name="LocalFile">Specifies the local file to download to.</param> /// <param name="FileMode">Specifies the file mode.</param> /// <param name="AppendFrom">Specifies where to append from.</param> /// <returns>Returns True if the request was successful, False otherwise.</returns> public bool DownloadFile(string RemoteFile, string LocalFile, DataConnection.FileModes FileMode, long AppendFrom) { return(DownloadFile(RemoteFile, LocalFile, StreamModes.Binary, DataConnection.FileModes.Overwrite, AppendFrom)); }