Esempio n. 1
0
        public void ChangeDir(string dirName)
        {
            //
            if (dirName == null || dirName.Equals(".") || dirName.Length == 0)
            {
                return;
            }
            if (!LoggedIn)
            {
                Login();
            }
            FTPResponse response = sendCommand("CWD " + dirName);

            if (response.Code != 250)
            {
                throw new ApplicationException(response.Message);
            }
            response = sendCommand("PWD");
            if (response.Code != 257)
            {
                throw new ApplicationException(response.Message);
            }
            this.mRemotePath = response.Message.Split('"')[1];
            FTPLog.LogMessage("Current directory is " + this.mRemotePath);
        }
Esempio n. 2
0
        private FTPResponse receive()
        {
            //Read a response from the socket receive buffer
            FTPResponse response = new FTPResponse();
            string      message  = "";

            do
            {
                byte[] buffer = new byte[BUFFER_SIZE];
                int    bytes  = this.mClientSocket.Receive(buffer, buffer.Length, SocketFlags.None);
                message += Encoding.ASCII.GetString(buffer, 0, bytes);
                Thread.Sleep(100);
            }while(this.mClientSocket.Available > 0);
            FTPLog.LogMessage("MESSAGE\t" + message);
            if (message.Length > 0)
            {
                string[] msgs = message.Split('\n');
                message = (msgs.Length > 2) ? msgs[msgs.Length - 2] : msgs[0];
                if (message.Length > 4 && (message.Substring(3, 1).Equals(" ") || message.Substring(3, 1).Equals("-")))
                {
                    response = new FTPResponse(message);
                }
            }
            return(response);
        }
Esempio n. 3
0
        private FTPResponse sendCommand(string command)
        {
            //
            FTPLog.LogMessage("COMMAND\t" + command);
            Byte[] bytes = Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray());
            this.mClientSocket.Send(bytes, bytes.Length, 0);
            FTPResponse response = receive();

            FTPLog.LogMessage("RESPONSE\t" + response);
            return(response);
        }
Esempio n. 4
0
        public void RemoveDir(string dirName)
        {
            //
            if (!LoggedIn)
            {
                Login();
            }
            FTPResponse response = sendCommand("RMD " + dirName);

            if (response.Code != 250)
            {
                throw new ApplicationException(response.Message);
            }
            FTPLog.LogMessage("Removed directory " + dirName);
        }
Esempio n. 5
0
        public void DeleteFile(string fileName)
        {
            //
            if (!LoggedIn)
            {
                Login();
            }
            FTPResponse response = sendCommand("DELE " + fileName);

            if (response.Code != 250)
            {
                throw new ApplicationException(response.Message);
            }
            FTPLog.LogMessage("Deleted file " + fileName);
        }
Esempio n. 6
0
        public void MakeDir(string dirName)
        {
            //
            if (!LoggedIn)
            {
                Login();
            }
            FTPResponse response = sendCommand("MKD " + dirName);

            if (response.Code != 250 && response.Code != 257)
            {
                throw new ApplicationException(response.Message);
            }
            FTPLog.LogMessage("Created directory " + dirName);
        }
Esempio n. 7
0
        public void RenameFile(string oldFileName, string newFileName, bool overwrite)
        {
            //
            if (!LoggedIn)
            {
                Login();
            }
            FTPResponse response = sendCommand("RNFR " + oldFileName);

            if (response.Code != 350)
            {
                throw new ApplicationException(response.Message);
            }
            if (!overwrite && GetFileList(newFileName).Length > 0)
            {
                throw new ApplicationException("File already exists");
            }
            response = sendCommand("RNTO " + newFileName);
            if (response.Code != 250)
            {
                throw new ApplicationException(response.Message);
            }
            FTPLog.LogMessage("Renamed file " + oldFileName + " to " + newFileName);
        }
Esempio n. 8
0
        public void Upload(string fileName, bool resume)
        {
            //
            if (!LoggedIn)
            {
                Login();
            }
            Socket     cSocket = null;
            FileStream input   = null;
            long       offset  = 0;

            if (resume)
            {
                try {
                    BinaryMode = true;
                    offset     = GetFileSize(Path.GetFileName(fileName));
                }
                catch (Exception) { offset = 0; }
            }
            try {
                //Open stream to read file
                input = new FileStream(fileName, FileMode.Open);
                if (resume && input.Length < offset)
                {
                    //Different file size
                    offset = 0;
                    FTPLog.LogMessage("Overwriting " + fileName);
                }
                else if (resume && input.Length == offset)
                {
                    // file done
                    input.Close();
                    FTPLog.LogMessage("Skipping completed " + fileName + " - turn resume off to not detect.");
                    return;
                }
                // dont create untill we know that we need it
                cSocket = createDataSocket();
                FTPResponse r;
                if (offset > 0)
                {
                    r = sendCommand("REST " + offset);
                    if (r.Code != 350)
                    {
                        FTPLog.LogMessage("Resuming not supported");
                        offset = 0;
                    }
                }
                r = sendCommand("STOR " + Path.GetFileName(fileName));
                if (r.Code != 125 && r.Code != 150)
                {
                    throw new ApplicationException(r.Message);
                }
                if (offset != 0)
                {
                    FTPLog.LogMessage("Resuming at offset " + offset);
                    input.Seek(offset, SeekOrigin.Begin);
                }
                FTPLog.LogMessage("Uploading file " + fileName + " to " + this.mRemotePath);
                byte[] buffer = new byte[BUFFER_SIZE];
                int    bytes  = 0;
                while ((bytes = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    cSocket.Send(buffer, bytes, 0);
                }
            }
            catch (Exception ex) { throw ex; }
            finally { input.Close(); if (cSocket.Connected)
                      {
                          cSocket.Close();
                      }
            }
            FTPResponse response = receive();

            if (response.Code != 226 && response.Code != 250)
            {
                throw new ApplicationException(response.Message);
            }
        }
Esempio n. 9
0
        public void Download(string remFileName, string locFileName, Boolean resume)
        {
            if (!LoggedIn)
            {
                Login();
            }
            BinaryMode = true;
            FTPLog.LogMessage("Downloading file " + remFileName + " from " + this.mServerName + "/" + this.mRemotePath);
            if (locFileName.Equals(""))
            {
                locFileName = remFileName;
            }
            FileStream output = null;

            if (!File.Exists(locFileName))
            {
                output = File.Create(locFileName);
            }
            else
            {
                output = new FileStream(locFileName, FileMode.Open);
            }
            Socket cSocket = createDataSocket();
            long   offset  = 0;

            if (resume)
            {
                offset = output.Length;
                if (offset > 0)
                {
                    FTPResponse r = sendCommand("REST " + offset);
                    if (r.Code != 350)
                    {
                        //Server dosnt support resuming
                        offset = 0;
                        FTPLog.LogMessage("Resuming not supported:" + r.Message);
                    }
                    else
                    {
                        FTPLog.LogMessage("Resuming at offset " + offset);
                        output.Seek(offset, SeekOrigin.Begin);
                    }
                }
            }
            FTPResponse response = sendCommand("RETR " + remFileName);

            if (response.Code != 150 && response.Code != 125)
            {
                throw new ApplicationException(response.Message);
            }
            DateTime timeout = DateTime.Now.AddSeconds(this.mTimeoutSec);

            byte[] buffer = new byte[BUFFER_SIZE];
            while (timeout > DateTime.Now)
            {
                int bytes = cSocket.Receive(buffer, buffer.Length, 0);
                output.Write(buffer, 0, bytes);
                if (bytes <= 0)
                {
                    break;
                }
            }
            output.Close();
            if (cSocket.Connected)
            {
                cSocket.Close();
            }
            response = receive();
            if (response.Code != 226 && response.Code != 250)
            {
                throw new ApplicationException(response.Message);
            }
        }
Esempio n. 10
0
 public void Close()
 {
     FTPLog.LogMessage("Closing connection to " + this.mServerName);
     sendCommand("QUIT");
     cleanup();
 }
Esempio n. 11
0
        public void Login()
        {
            //
            if (LoggedIn)
            {
                Close();
            }
            FTPLog.LogMessage("Opening connection to " + this.mServerName);
            IPHostEntry iphe = Dns.GetHostEntry(this.mServerName);

            foreach (IPAddress address in iphe.AddressList)
            {
                IPEndPoint ipe    = new IPEndPoint(address, this.mPortNumber);
                Socket     socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                try { socket.Connect(ipe); } catch { }
                if (socket.Connected)
                {
                    this.mClientSocket = socket;
                    break;
                }
                else
                {
                    continue;
                }
            }
            if (this.mClientSocket == null)
            {
                throw new ApplicationException("Couldn't connect to remote server " + this.mServerName + ".");
            }

            FTPResponse response = receive();

            if (response.Code != 0)
            {
                if (response.Code != 220)
                {
                    Close();
                    throw new ApplicationException(response.Message);
                }
            }
            else
            {
                throw new ApplicationException("No response from ftp sever on connecting.");
            }

            if (this.mUsername.Length > 0)
            {
                response = sendCommand("USER " + this.mUsername);
                if (response.Code != 0)
                {
                    if (!(response.Code == 331 || response.Code == 230))
                    {
                        cleanup();
                        throw new ApplicationException(response.Message);
                    }
                }
                else
                {
                    throw new ApplicationException("No response from ftp sever on getting username.");
                }
            }
            if (response.Code != 230)
            {
                response = sendCommand("PASS " + this.mPassword);
                if (response.Code != 0)
                {
                    if (!(response.Code == 230 || response.Code == 202))
                    {
                        cleanup();
                        throw new ApplicationException(response.Message);
                    }
                }
                else
                {
                    throw new ApplicationException("No response from ftp sever on setting password.");
                }
            }

            FTPLog.LogMessage("Connected to " + this.mServerName);
            ChangeDir(this.mRemotePath);
        }