private IFtpChannel OpenDataChannel() { FtpServerResponse response = SendCommand("PASV"); if (response.ReturnCode != (int)FtpReturnCode.EnteringPassiveMode) { throw FtpException("Could not upload file", response); } string responseMessage = response.Message; int openBracketIndex = responseMessage.IndexOf('('); if (openBracketIndex < 0) { throw new FtpException("Unexpected response message: '{0}'".Fmt(responseMessage)); } int start = openBracketIndex + 1; int closeBracketIndex = responseMessage.IndexOf(')', start); if (closeBracketIndex < 0) { throw new FtpException("Unexpected response message: '{0}'".Fmt(responseMessage)); } int length = closeBracketIndex - start; string[] splits = responseMessage.Substring(start, length).Split(','); Contract.Assert(splits.Length >= 6); byte[] addressBytes = new byte[4]; for (int i = 0; i < 4; i++) { addressBytes[i] = byte.Parse(splits[i], CultureInfo.InvariantCulture); } int dataChannelPort = int.Parse(splits[4], CultureInfo.InvariantCulture) * 256 + int.Parse(splits[5], CultureInfo.InvariantCulture); IFtpChannel dataChannel = ftpChannelFactory.CreateChannel(); dataChannel.Connect(addressBytes, dataChannelPort); return(dataChannel); }
public void UploadFileWithoutChecks(string localFileName, string remoteFileName) { Contract.Requires(localFileName != null); Contract.Requires(remoteFileName != null); FtpServerResponse response; IFtpChannel dataChannel = null; try { dataChannel = OpenDataChannel(); response = SendCommand("STOR {0}", remoteFileName); if (response.ReturnCode != (int)FtpReturnCode.DataConnectionAlreadyOpen && response.ReturnCode != (int)FtpReturnCode.FileStatusOk) { throw FtpException("Could not upload file", response); } byte[] data = fileSystem.ReadFileAsBytes(localFileName); if (dataChannel.Send(data) != data.Length) { throw new FtpException("Not all of file was uploaded"); } } finally { if (dataChannel != null) { dataChannel.Disconnect(); ftpChannelFactory.Destroy(dataChannel); } } response = ftpCommunicator.ReadResponse(); if (response.ReturnCode != (int)FtpReturnCode.RequestedFileActionOkayCompleted && response.ReturnCode != (int)FtpReturnCode.ClosingDataConnection) { throw FtpException("Could not upload file", response); } }
void IFtpCommunicator.AttachToChannel(IFtpChannel channel) { Contract.Requires(channel != null); }
public void AttachToChannel(IFtpChannel channel) { this.channel = channel; }