// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Text; using System.Net.Sockets; using System.Threading; public class FtpClient { private TcpClient ctrl; private NetworkStream ctrlstm; private TcpClient data; private NetworkStream datastm; private static void Send(NetworkStream stm, string line) { byte[] b = Encoding.Default.GetBytes(line); stm.Write(b, 0, b.Length); } private static string Receive(NetworkStream stm) { byte[] b = new byte[100000]; int ix = 0; int n; while(stm.DataAvailable) { n = stm.Read(b, ix, b.Length - ix); ix += n; } return Encoding.Default.GetString(b, 0, ix); } public static string Command(NetworkStream stm, string cmd) { Send(stm, cmd + "\r\n"); Thread.Sleep(50); return Receive(stm); } private void SetupData() { string dataaddr = Command(ctrlstm, "PASV"); string[] addrparts = dataaddr.Split("()".ToCharArray())[1].Split(",".ToCharArray()); string datahost = addrparts[0] + "." + addrparts[1] + "." + addrparts[2] + "." + addrparts[3]; int dataport = int.Parse(addrparts[4]) * 256 + int.Parse(addrparts[5]); data = new TcpClient(datahost, dataport); datastm = data.GetStream(); } public FtpClient(string host, string username, string password) { ctrl = new TcpClient(host, 21); ctrlstm = ctrl.GetStream(); Command(ctrlstm, "USER " + username); Command(ctrlstm, "PASS " + password); } public string Dir() { SetupData(); Command(ctrlstm, "LIST"); string res = Receive(datastm); Receive(ctrlstm); datastm.Close(); data.Close(); return res; } public string ShortDir() { SetupData(); Command(ctrlstm, "NLST"); string res = Receive(datastm); Receive(ctrlstm); datastm.Close(); data.Close(); return res; } public void MkDir(string dir) { Command(ctrlstm, "MKD " + dir); } public void RmDir(string dir) { Command(ctrlstm, "RMD " + dir); } public void ChDir(string dir) { Command(ctrlstm, "CWD " + dir); } public void UpLoad(string filename, bool binary) { if(binary) { Command(ctrlstm, "TYPE I"); } else { Command(ctrlstm, "TYPE A"); } SetupData(); Command(ctrlstm, "STOR " + filename); Receive(ctrlstm); FileStream f = new FileStream(filename, FileMode.Open); byte[] b = new byte[100000]; int ix = 0; int n; while(ix < f.Length) { n = f.Read(b, 0, b.Length); datastm.Write(b, 0, n); Thread.Sleep(100); ix += n; } f.Close(); datastm.Close(); data.Close(); } public void DownLoad(string filename, bool binary) { if(binary) { Command(ctrlstm, "TYPE I"); } else { Command(ctrlstm, "TYPE A"); } SetupData(); Command(ctrlstm, "RETR " + filename); Receive(ctrlstm); FileStream f = new FileStream(filename, FileMode.Create); byte[] b = new byte[100000]; int n; while(datastm.DataAvailable) { n = datastm.Read(b, 0, b.Length); f.Write(b, 0, n); Thread.Sleep(100); } f.Close(); datastm.Close(); data.Close(); } public void Logout() { Command(ctrlstm, "QUIT"); ctrlstm.Close(); ctrl.Close(); } } class TestClass { public static void Main(string[] args) { FtpClient cli = new FtpClient("localhost", "anonymous", "arne@"); Console.WriteLine(cli.ShortDir()); Console.WriteLine(cli.Dir()); Directory.SetCurrentDirectory("C:\\"); cli.DownLoad("z.zip", true); cli.MkDir("subdir"); cli.ChDir("subdir"); cli.UpLoad("z.zip", true); cli.Logout(); } }