Example #1
0
        public long DownloadFile(string reomteFileName, Stream fs_for_save)
        {
            if (fs_for_save == null)
            {
                return(-2);
            }
            if (!this.connected_flag)
            {
                return(-3);
            }

            FileReceiver fr = new FileReceiver();

            fr.receiver_stream = fs_for_save;
            fr.Receive(fs_for_save);

            System.Windows.Forms.MessageBox.Show("Start listen to port");

            IPEndPoint localIP     = (IPEndPoint)this.cmdSocket.LocalEndPoint;
            string     localIP_str = localIP.Address.ToString();

            FTP_response res_port = this.SendCmd_toServer("PORT " + localIP_str.Replace('.', ',') + ",10,107");
            FTP_response res_retr = this.SendCmd_toServer("RETR " + reomteFileName);

            return(-1);
        }
Example #2
0
        public bool Login(string serverAddr, string userID, string pwd)
        {
            this.cmdSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            /*Initial connection to remote server*/
            try { this.cmdSocket.Connect(serverAddr, this.cmdPort); }
            catch (Exception ex)
            {
                if (this.errorLog != null)
                {
                    this.errorLog.Add("Native_FTP error 0", "can't connect to server\r\n" + ex.ToString());
                }
                return(this.connected_flag = false);
            }

            FTP_response res_init = this.ReadMsg_fromServer();

            if (res_init.statusCode != 220) //220 Service ready for new user.
            {
                if (this.errorLog != null)
                {
                    this.errorLog.Add("Native_FTP error 1", res_init.message);
                }
                return(this.connected_flag = false);
            }

            /*Send user ID*/
            FTP_response res_uid = this.SendCmd_toServer("USER " + userID);

            for (int i = 0; i < this.conn_retryTimes && res_uid.statusCode == 220; ++i)
            {
                res_uid = this.SendCmd_toServer("USER " + userID);
            }
            if (res_uid.statusCode != 331) //331 User name okay, need password.
            {
                if (this.errorLog != null)
                {
                    this.errorLog.Add("Native_FTP error 2", res_uid.message);
                }
                return(this.connected_flag = false);
            }

            /*Send password*/
            FTP_response res_pwd = this.SendCmd_toServer("PASS " + pwd);

            for (int i = 0; i < this.conn_retryTimes && res_pwd.statusCode == 331; ++i)
            {
                res_pwd = this.SendCmd_toServer("PASS " + pwd);
            }
            if (res_pwd.statusCode != 230) //230 User logged in, proceed. Logged out if appropriate.
            {
                if (this.errorLog != null)
                {
                    this.errorLog.Add("Native_FTP error 3", res_pwd.message);
                }
                return(this.connected_flag = false);
            }

            //FTP_response res_tp = this.SendCmd_toServer("TYPE I");//binary type

            return(this.connected_flag = true);
        }