Exemple #1
0
        public FTPClient(IPAddress serverIP, int localControlPort, string user, string password, Action <string> ConsoleLogDelegate)
        {
            controlClient    = new TcpClient();
            dataClient       = new TcpClient();
            ConsoleLogEvent += ConsoleLogDelegate;
            controlPort      = localControlPort;
            dataPort         = controlPort + 1;
            try
            {
                controlClient.Connect(serverIP, MyFTPHelper.ftpControlPort);
                controlStream = controlClient.GetStream();

                FTPCommand userCommand = new FTPCommand("USER", new string[] { user });
                if (!SendMessageToServerAndWaitReply(userCommand.ToString() + MyFTPHelper.FTPNewLine))
                {
                    return;
                }
                FTPCommand passwordCommand = new FTPCommand("PASS", new string[] { password });
                if (!SendMessageToServerAndWaitReply(passwordCommand.ToString() + MyFTPHelper.FTPNewLine))
                {
                    return;
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
Exemple #2
0
        public void DownLoadFile(string filename, int size)
        {
            FTPCommand portCommand = new FTPCommand("PORT", new string[] { controlPort.ToString() });

            if (!SendMessageToServerAndWaitReply(portCommand.ToString() + MyFTPHelper.FTPNewLine))
            {
                return;
            }
            ListenDataPort();
            PostMessageToConsoleWithLock("开始下载" + filename);
            FTPCommand downloadCommand = new FTPCommand("RETR", new string[] { filename });

            if (!SendMessageToServerAndWaitReply(downloadCommand.ToString() + MyFTPHelper.FTPNewLine))
            {
                return;
            }

            try
            {
                FileStream    fs         = File.OpenWrite(downloadDirectory + filename);
                NetworkStream dataStream = dataClient.GetStream();
                currenttransfer = new FileTransfer()
                {
                    networkStream = dataStream,
                    filestream    = fs,
                };
                currenttransfer.DownloadAsync(() =>
                {
                    fs.Close();
                    dataStream.Close();
                    PostMessageToConsoleWithLock("完成下载");
                });
            }
            catch (Exception exc)
            {
                PostMessageToConsoleWithLock(exc.Message);
            }
        }
Exemple #3
0
        public void UploadFile(string filepath)
        {
            FTPCommand portCommand = new FTPCommand("PORT", new string[] { controlPort.ToString() });

            if (!SendMessageToServerAndWaitReply(portCommand.ToString() + MyFTPHelper.FTPNewLine))
            {
                return;
            }
            ListenDataPort();

            PostMessageToConsoleWithLock("开始上传" + Path.GetFileName(filepath));
            FTPCommand uploadCommand = new FTPCommand("STOR", new string[] { Path.GetFileName(Path.GetFileName(filepath)) });

            if (!SendMessageToServerAndWaitReply(uploadCommand.ToString() + MyFTPHelper.FTPNewLine))
            {
                return;
            }
            try
            {
                FileStream    fs         = File.OpenRead(filepath);
                NetworkStream dataStream = dataClient.GetStream();
                currenttransfer = new FileTransfer()
                {
                    networkStream = dataStream,
                    filestream    = fs,
                };
                currenttransfer.UploadAsync(() =>
                {
                    fs.Close();
                    dataStream.Close();
                    PostMessageToConsoleWithLock("完成上传");
                });
            }
            catch (Exception exc)
            {
                PostMessageToConsoleWithLock(exc.Message);
            }
        }
Exemple #4
0
        public void UpdateList()
        {
            FTPCommand fileListCommand = new FTPCommand("LIST", null);

            SendMessageToServerAndWaitReply(fileListCommand.ToString() + MyFTPHelper.FTPNewLine);
        }