Skip to content
Snippets Groups Projects
FtpClient.cs 6.02 KiB
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace FTPClient
{
    public class FtpClient
    {
        public static Socket socket;
        public static Socket ftpSocket;
        public static IPEndPoint ipEndp;
        public static IPEndPoint ftpIpEndp;
        public static NetworkStream ns;
        public static NetworkStream ftpNs;
        public static StreamReader sr;
        public static StreamReader ftpSr;
        public static StreamWriter sw;
        public static StreamWriter ftpSw;
        public string status;

        public string username { get; set; }
        public string password { get; set; }
        public string host { get; set; }
        public int port { get; set; }
        public int ftpPort { get; set; }

        // Constructor
        public FtpClient(string username, string password, string host, int port)
        {
            this.username = username;
            this.password = password;
            this.host = host;
            this.port = port;
            this.ftpPort = -1;
        }

        // Initialize socket and connect to server
        public void Initialize()
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress[] ips = Dns.GetHostAddresses(host);
            IPAddress address = ips[1];
            ipEndp = new IPEndPoint(address, port);
            
            try
            {
                socket.Connect(ipEndp);
            } catch (Exception e)
            {
                Console.Write(e);
                Console.ReadKey();
            }

            ns = new NetworkStream(socket);
            sr = new StreamReader(ns);
            sw = new StreamWriter(ns);
            string response = sr.ReadLine();
            handleResponse(response);
        }

        private void CreateSocket()
        {
            ftpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress[] ips = Dns.GetHostAddresses(host);
            IPAddress address = ips[1];
            ftpIpEndp = new IPEndPoint(address, ftpPort);
            try
            {
                ftpSocket.Connect(ftpIpEndp);
            }
            catch (Exception e)
            {
                Console.Write(e);
                Console.ReadKey();
            }

            ftpNs = new NetworkStream(ftpSocket);
            ftpSr = new StreamReader(ftpNs);
            ftpSw = new StreamWriter(ftpNs);
        }

        private void handleEpsvResponse(string response)
        {
            status = response.Substring(0, 3);

            if (status == "229")
            {
                string newPort = response.Substring(0, response.LastIndexOf('|'));
                int lastIndex = newPort.LastIndexOf('|');
                int endIndex = newPort.Length - lastIndex - 1;
                newPort = newPort.Substring(lastIndex+1, endIndex);
                int portInt = int.Parse(newPort);

                this.ftpPort = portInt;

                CreateSocket();
            }

            handleResponse(response);
        }

        private void handleResponse(string response)
        {
            status = response.Substring(0, 3);
            bool loop = true;
            string resp = String.Empty;
            Console.WriteLine(response);
            if(response[3] == '-')
            {
                while (loop)
                {
                    resp = sr.ReadLine();
                    if (resp[3] == '-')
                    {
                        Console.WriteLine(resp);
                    }
                    else
                    {
                        Console.WriteLine(resp);
                        loop = false;
                    }
                }
            }
        }

        public void LogIn()
        {
            bool keskustelu = true;
            while (keskustelu)
            {
                sw.WriteLine("USER " + username);
                sw.Flush();
                handleResponse(sr.ReadLine());
                switch (status)
                {
                    case "220":
                        sw.WriteLine("USER " + username);
                        sw.Flush();
                        handleResponse(sr.ReadLine());
                        break;
                    case "230":
                        keskustelu = false;
                        break;
                    case "331":
                        sw.WriteLine("PASS " + password);
                        sw.Flush();
                        handleResponse(sr.ReadLine());
                        break;
                    default:
                        Console.WriteLine("Error in login");
                        break;
                }
            }
        }

        public void Epsv()
        {
            sw.WriteLine("EPSV");
            sw.Flush();
            handleEpsvResponse(sr.ReadLine());
        }

        public void List()
        {
            sw.WriteLine("LIST");
            sw.Flush();
            handleResponse(sr.ReadLine());
            bool listen = true;
            while(listen)
            {
                Console.WriteLine(ftpSr.ReadLine());
            }
        }

        public void Quit()
        {
            sw.WriteLine("QUIT");
            sw.Flush();
            handleResponse(sr.ReadLine());
            TearDown();
        }

        private void TearDown()
        {
            sw.Close();
            sr.Close();
            ns.Close();
            socket.Close();
        }

        public void GetDirectory()
        {
            try
            {   
                socket.Connect(ipEndp);
            } catch (Exception e)
            {
                Console.Write(e);
                Console.ReadKey();
            }

            NetworkStream ns = new NetworkStream(socket);

            StreamReader sr = new StreamReader(ns);
            StreamWriter sw = new StreamWriter(ns);
            string viesti = sr.ReadLine();
            Console.WriteLine(viesti);
        }

    }
}