Example #1
0
        private long FileSize(string FileName)
        {
            if (!Logged)
            {
                Status_text.AppendText("Login First\n");
                return(0);
            }
            SendCommand("SIZE " + FileName);
            long Filesize = long.Parse(Result.Substring(4));

            return(Filesize);
        }
Example #2
0
 private void ChangeDir(string Path)
 {
     if (!Logged)
     {
         Status_text.AppendText("Login First\n");
         Changed = false;
         return;
     }
     SendCommand("CWD " + Path);
     SendCommand("PWD");
     Path = StatusMessage.Split('"')[1];
     Status_text.AppendText("Current Working Directory is " + Path + "\n");
     Changed = true;
 }
Example #3
0
 private string SplitResponse()
 {
     try
     {
         while (true)
         {
             Bytes          = FTPSocket.Receive(Buffer, Buffer.Length, 0);
             StatusMessage += Encoding.ASCII.GetString(Buffer, 0, Bytes);
             if (Bytes < Buffer.Length)
             {
                 break;
             }
         }
         string[] msg = StatusMessage.Split('\n');
         if (StatusMessage.Length > 2)
         {
             StatusMessage = msg[msg.Length - 2];
         }
         else
         {
             StatusMessage = msg[0];
         }
         if (!StatusMessage.Substring(3, 1).Equals(" "))
         {
             return(SplitResponse());
         }
         for (int i = 0; i < msg.Length - 1; i++)
         {
             Status_text.AppendText(msg[i] + "\n");
         }
         return(StatusMessage);
     }
     catch (Exception ex)
     {
         Status_text.AppendText("Status : ERROR. " + ex.Message + "\n");
         FTPSocket.Close();
         return("");
     }
 }
Example #4
0
        private void Login()
        {
            if (Logged)
            {
                CloseConnection();
            }
            IPAddress  remoteAddress   = null;
            IPEndPoint addressEndPoint = null;

            Status_text.AppendText("Opening Connection to: " + Server + "\n");

            try
            {
                FTPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Status_text.AppendText("Resolving IP Address\n");
                remoteAddress = Dns.GetHostEntry(Server).AddressList[0];
                Status_text.AppendText("IP Address Found -> " + remoteAddress.ToString() + "\n");
                addressEndPoint = new IPEndPoint(remoteAddress, Port);
                Status_text.AppendText("EndPoint Found -> " + addressEndPoint.ToString() + "\n");
                FTPSocket.Connect(addressEndPoint);
                SendCommand("USER " + UserName);
                SendCommand("PASS " + Password);
                Logged = true;
                Status_text.AppendText("Connected to " + Server + "\n");
            }
            catch (Exception ex)
            {
                if (FTPSocket != null && FTPSocket.Connected)
                {
                    FTPSocket.Close();
                }
                Status_text.AppendText("Couldn't connect to remote server. " + ex.Message + "\n");
                return;
            }
            ReadResponse();
            ChangeDir(Path);
        }
Example #5
0
        public string[] GetListFiles()
        {
            DataSock = OpenSocketForTransfer();
            if (DataSock == null)
            {
                Status_text.AppendText("Socket Error\n");
                return(Msg);
            }
            SendCommand("MLSD");
            StatusMessage = "";
            DateTime timeout = DateTime.Now.AddSeconds(30);

            while (timeout > DateTime.Now)
            {
                int Bytes = DataSock.Receive(Buffer, Buffer.Length, 0);
                StatusMessage += Encoding.ASCII.GetString(Buffer, 0, Bytes);
                if (Bytes < Buffer.Length)
                {
                    break;
                }
            }
            Msg = StatusMessage.Replace("\r", "").Split('\n');
            DataSock.Close();
            if (StatusMessage.Contains("No files found"))
            {
                Msg = new string[] { }
            }
            ;
            ReadResponse();
            if (StatusCode != 226)
            {
                Msg = new string[] { }
            }
            ;
            return(Msg);
        }
Example #6
0
        private Socket OpenSocketForTransfer()
        {
            SendCommand("PASV");
            Socket     tranferSocket = null;
            IPEndPoint ipEndPoint    = null;
            int        indx1         = Result.IndexOf('(');
            int        indx2         = Result.IndexOf(')');
            string     IpPort        = Result.Substring((indx1 + 1), (indx2 - indx1) - 1);

            int[]  Parts     = new int[6];
            int    PartCount = 0;
            string Buffer    = "";

            for (int i = 0; i < IpPort.Length && PartCount <= 6; i++)
            {
                char chr = char.Parse(IpPort.Substring(i, 1));
                if (char.IsDigit(chr))
                {
                    Buffer += chr;
                }
                else if (chr != ',')
                {
                    Status_text.AppendText("Wrong PASV result -> " + Result);
                    return(null);
                }
                else
                {
                    if (chr == ',' || i + 1 == IpPort.Length)
                    {
                        try
                        {
                            Parts[PartCount++] = int.Parse(Buffer);
                            Buffer             = "";
                        }
                        catch (Exception)
                        {
                            Status_text.AppendText("Wrong PASV result: " + Result + "\n");
                            return(null);
                        }
                    }
                }
            }
            Parts[PartCount] = int.Parse(Buffer);
            string ipAddress = Parts[0] + "." + Parts[1] + "." + Parts[2] + "." + Parts[3];
            int    port      = (Parts[4] << 8) + Parts[5];

            try
            {
                tranferSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ipEndPoint    = new IPEndPoint(IPAddress.Parse(ipAddress), port);
                tranferSocket.Connect(ipEndPoint);
            }
            catch (Exception ex)
            {
                if (tranferSocket != null && tranferSocket.Connected)
                {
                    tranferSocket.Close();
                }
                Status_text.AppendText("Status : Can't connect to remote server ->" + ex.Message + " \n");
                return(null);
            }
            return(tranferSocket);
        }
Example #7
0
 private void Status_text_TextChanged(object sender, EventArgs e)
 {
     Status_text.ScrollToCaret();
 }