Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region FTPClient
- public class FTPClient
- {
- #region Objects
- private string p_Host, p_Path, p_User, p_Password;
- private int p_Port;
- private Socket p_Socket;
- private string p_Reply;
- private int p_ReplyCode;
- private bool p_Connected;
- #endregion
- #region Properties
- public string Host
- {
- get { return p_Host; }
- }
- public string Path
- {
- get { return p_Path; }
- }
- public string User
- {
- get { return p_User; }
- }
- public string Password
- {
- get { return p_Password; }
- }
- public int Port
- {
- get { return p_Port; }
- }
- public Socket Socket
- {
- get { return p_Socket; }
- }
- public string Reply
- {
- get { return p_Reply; }
- }
- public int ReplyCode
- {
- get { return p_ReplyCode; }
- }
- public bool Connected
- {
- get { return p_Connected; }
- }
- #endregion
- #region Constructor/Destructor
- public FTPClient(string host, string path, string user, string password, int port)
- {
- p_Host = host;
- p_Path = path;
- p_User = user;
- p_Password = password;
- p_Port = port;
- p_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- p_Reply = string.Empty;
- p_ReplyCode = 0;
- p_Connected = false;
- }
- ~FTPClient()
- {
- p_Connected = false;
- p_ReplyCode = 0;
- p_Reply = string.Empty;
- p_Socket = null;
- p_Port = 0;
- p_Password = string.Empty;
- p_User = string.Empty;
- p_Path = string.Empty;
- p_Host = string.Empty;
- }
- #endregion
- #region Public Methods
- public bool Connect()
- {
- IPEndPoint ep = new IPEndPoint(Dns.Resolve(p_Host).AddressList[0], p_Port);
- try
- {
- p_Socket.Connect(ep);
- ReadReply();
- if (p_ReplyCode != 220) Disconnect();
- else
- {
- SendCommand("USER " + p_User);
- if (p_ReplyCode != 331 || p_ReplyCode == 230) Cleanup();
- if (p_ReplyCode != 230)
- {
- SendCommand("PASS " + p_Password);
- if (p_ReplyCode != 230 || p_ReplyCode == 202) Cleanup();
- }
- ChangeDirectory(p_Path);
- p_Connected = true;
- }
- }
- catch (SocketException)
- {
- Cleanup();
- }
- return p_Connected;
- }
- public bool ChangeDirectory(string path)
- {
- bool result = true;
- if (p_Connected)
- {
- p_Path = path;
- SendCommand("CWD " + p_Path);
- if (p_ReplyCode != 250) result = false;
- }
- else result = false;
- return result;
- }
- public void Disconnect()
- {
- if (p_Socket != null && p_Connected) SendCommand("QUIT");
- Cleanup();
- }
- #endregion
- #region Private Methods
- private void Cleanup()
- {
- if (p_Socket != null) p_Socket.Close();
- p_Connected = false;
- }
- private void ReadReply()
- {
- p_Reply = ReadLine();
- p_ReplyCode = Int32.Parse(p_Reply.Substring(0, 3));
- }
- private string ReadLine()
- {
- string message = string.Empty;
- while (true)
- {
- p_Buffer.Clear(p_Buffer, 0, BLOCK_SIZE);
- p_Bytes = p_Socket.Receive(p_Buffer, p_Buffer.Length, 0);
- p_Message += Encoding.ASCII.GetString(p_Buffer, 0, p_Bytes);
- if (p_Bytes < p_Buffer.Length) break;
- }
- string[] s = message.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
- }
- private void SendCommand(string command)
- {
- command += Environment.NewLine;
- byte[] data = Encoding.ASCII.GetBytes(command);
- p_Socket.Send(data, data.Length, 0);
- ReadReply();
- }
- #endregion
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement