FTP Client Library In C #

zhaozj2021-02-16  94

FTP Client Library IN C # Author: Jaimon Mathew

This is the C # version of an FTP client library which is originally written in Java. The library will be compiled to a DLL file. A test program is also included to show the usage of this library. You will get more details about FTP from its RFC. Most of the Commands Aresu Supported Here. File Upload & Download The Commands Are Capable Enough of Doing Resuming Also.

The Given Test Program Is A Console Based Application. I 抦 Inviting Somebody On The Net To Develop a Front End Application with this library.

/ * FTPFactory.cs Better view with tab space = 4 Written by Jaimon Mathew (jaimonmathew@rediffmail.com) Rolander, Dan (Dan.Rolander@marriott.com) has modified the download method to cope with file name with path information. He . also provided the XML comments so that the library provides Intellisense descriptions use the following line to compile csc / target: library /out:FTPLib.dll /r:System.DLL FTPFactory.cs * / using System; using System.Net; using System.IO; using System.Text; using System.Net.Sockets; namespace ftpLib {public class FTPFactory {private string remoteHost, remotePath, remoteUser, remotePass, mes; private int remotePort, bytes; private Socket clientSocket; private int retValue; private Boolean debug; private Boolean logined; private string reply; private static int BLOCK_SIZE = 512; Byte [] buffer = new Byte [BLOCK_SIZE]; Encoding ASCII = Encoding.ASCII; public FTPFactory () {remoteHost = "localhost"; remotePath = " Remoteuser = "Anonymous"; RemotePass = "jaimon@school2000.co .uk "; Remoteport = 21; Debug = false; logined = false;} // /// set the name of the ftp server to connection to. ///// Server name public void setRemotehost (String remotehost) { This.RemoteHost = Remotehost;} /// /// Return the name of the current ftp server. ///// Server name public string getremotehost () {return remotehost;} ///// set the port number To use for ftp. /// // port number public void setremoteport (int RemotEport) {this.remoteport = RemotEport;} // /// Return The Current Port Number. /// /// Current Port Number PUBLIC INT getRemotEport () {Return RemotEport;

} /// /// Set the remote directory path /// /// The remote directory path public void setRemotePath (string remotePath) {this.remotePath = remotePath;.} /// /// Return the current remote directory path . PUBLIC STRING GETREMOTEPATH () {Return RemotePath;} ///// set the user name to use for logging inTo the Remote Server. ///// UserName Public void setRemoteUser (string remoteUser) {this.remoteUser = remoteUser;} /// /// Set the password to user for logging into the remote server /// /// password public void setRemotePass (string remotePass) {this.remotePass. = RemotePass;} ///// Return A String Array Containing The Remote Directory's File List. //////// Public String [] getFileList (String Mask) {if (! logined) {login (); } Socket csocket = createDataSocket (); sendcommand ("NLST" Mask); if (! (RetValue == 150 || RetValue == 125)) {Throw new oException (reply.substring (4));} MES = " ";" {INT bytes = csocket.receive (buffer, Buffer.Length, 0); MES = ascii.getstring (buffer, 0, bytes); if (Bytes

.} /// /// Login to the remote server /// public void login () {clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ep = new IPEndPoint (Dns.Resolve ( Remotehost) .addressList [0], RemotePort); try {clientsocket.connect (EP); "COULDN't Connect To Remote Server);} ReadReply (); if (RetValue! = 220) {close (); throw new oException (reply.substring (4));} if (debug) Console.Writeline ("User"; "User" Remoteuser; if (! (! == 331 || RetValue == 230)) {cleanup (); throw new oException (reply.substring (4));} if (RetValue! = 230) {if (debug) Console.writeline ("pass xxx") Sendcommand ("pass" remotepass; if (! (rv == 230 || RetValue == 202)) {cleanup (); throw new oException (reply.substring (4));}} Logined = true; console; .Writeline ("Connected to" Remotehost; Chdir (RemotePath);} ///// if the value of mode is true, set binary mode for Downloads. /// else, set ascii Mode. /// // public void setbinarymode (Boolean Mode) {if (mode) {sendcommand ("Type I");} else {sendcommand ("Type A");} if (RetValue! = 200) {THROW New oException (reply.substring (4));}} /// /// Download a file to the assembly's local directory, /// Keeping the Same file name. ///// Public void Download (String Remfilename) {Download (REMFILENAME, "", FALSE);} ///// Download a Remote File to the assembly's local directory, // Keeping the Same File Name, and set the resume flag. /// /// / // public void Download (String Remfilename, Boolean ResMe) {Download (RemfileName, "

", resume;} // /// Download a Remote File to a local file name Which can include /// a path. The local file name will be created or overwritten, // but the path must exist. /// // /// /// public void download (string remFileName, string locFileName) {download (remFileName, locFileName, false);} /// /// Download a remote file to a local file name which can include /// a path, and set the resume. The local file name will be /// Created or overwritten, but the path must exist. /// /// / / / / / PUBLIC VOID DOWNLOAD (String Locilename, Boolean Resume) {if (! Logined) {login ();} setbinarymode (true); console.writeline ("Download File" RemfileName "from" Remotehost / " RemotePath); if (LocFileName.Equals (" ")) {locFileName = remFileName;} if (File.Exists (locFileName)) {Stream st = File.Create (locFileName); st.Close ();}! FileStream output = new FileStream (locFileName, FileMode.Open); Socket csocket = createDataSocket (); long offset = 0; if (resume) {offset = Output.l ENGTH; if (Offset> 0) {sendcommand; if (RetValue! = 350) {// throw new oException (reply.substring (4)); // Some Servers May Not Support Resuming. Offset = 0;}}}} {if (debug) {Console.writeline ("Seeking to" offset;} long npos = output.seek (offset, seekorigin.begin); console.writeline ("New POS = " npos);}} sendcommand (" retrin transfilename); if (! (RetValue == 150 || RetValue == 125)) {throw new oException (reply.substring (4));} while True) {bytes = csocket.receive (buffer, buffer.length, 0); Output.write (buffer, 0, bytes); if (Bytes <=

0) {Break;}} output.close (); if (csocket.Connected) {csocket.close ();} console.writeline (""); readreply (); if (! (! (RetValue == 226 || RetValue == 250)) {throw new oException (reply.substring (4));}} /////// PUBLIC VOID UPLOAD (STRING FILENAME) {UPLOAD (FileName, False) } ///// UPLOAD A File and set the resume flag. /////// Public void upload (String filename, Boolean resume) {if (! Logined) {login ();} Socket Csocket = createDataSocket (); long offset = 0; if (resume) {Try {setBinaryMode (true); offset = getFileSize (filename);} catch (exception) {offset = 0;}}}}}}}}}} (offset> 0) {Sendcommand "REST" offset); if (RetValue! = 350) {// throw new oException (reply.substring (4)); // Remote Server May Not Support Resuming. Offset = 0;}} SendCommand ("Stor" Path.GetFileName (FileName)); if (! (RetValue == 125 || RetValue == 150)) {throw new oException (reply.substring (4));} // open input stream to read source file filestream input = New FileStream (filename, fil Emode.open; if (offset! = 0) {if (debug) {Console.Writeline ("Seeking to" offset;} Input.seek (offset, seekorigin.begin);} console.writeline ("UPLoading File " FileName " To " RemotePath); While ((Bytes = INPUT.READ (Buffer, 0, Buffer.Length)> 0) {csocket.send (buffer, bytes, 0);} input.close (); Console.Writeline ("csocket.connected) {csocket.close ();} readreply (); if (! (Return == 226 || RetValue == 250)) {throw new oException (reply.substring (4));

}}} // /// delete a file from the remote ftp server. // /// public void deleteremotefile (String filename) {if (! Logined) {login ();} sendcommand ("dele" filename) ; if (RetValue! = 250) {throw new oException (reply.substring (4));}} /// /// renaMe a file on the remote ftp server. /////// Public void renameremotefile "String newfilename" {if (! logined) {login ();} Sendcommand ("RNFR" OldFileName; if (RetValue! = 350) {throw new oException (reply.substring (4));} / / known problem // rnto will not take care of existing file // ie It will overwrite if newFileName exist sendCommand ( "RNTO" newFileName);. if (retValue = 250!) {throw new IOException (reply.Substring (4) );}} /// /// Create a directory on the remote ftp server. ///// Public void mkdir (String Dirname) {if (! Logined) {login ();} sendcommand ("mkd" DirName); if (RetValue! = 250) {throw new oException (reply.substring (4));}} /// // delete a directory on the remote ftp server. //// // public void r MDIR ("STRING DIRNAME) {login ();} Sendcommand (" RMD " DirName); if (RetValue! = 250) {throw new oException (reply.substring (4));}} // / /// Change The Current Working Directory on The Remote FTP Server. //// (String DirName) {IF (DirName.Equals (".")) {Return;} if (! Logined) { SENDCOMMAND ("CWD" DIRNAME); if (RetValue! = 250) {throw new oException (reply.Substring (4));} this.RemotePath = DIRNAME; console.writeline ("Current Directory IS" RemotePath);} // /// Close the ftp connection. /// public void close () {if (clientsocket! = null) {Sendcommand ("quit");

} cleanup (); console.writeline ("closing ...");} /////} {this.debug = debug;} private Void ReadReply () {MES = ""; reply = readline (); retval = int32.parse (reply.substring (0,3));} private void cleanup () {if (ClientSocket! = null) {ClientSocket.close ();} logined = false;} private string readline () {while (true) {bytes = clientsocket.Receive (buffer, buffer.length, 0); MES = ascii.getstring (buffer, 0, Bytes); iftes 2) {MES = Mess [Mess.Length-2];} else {mes = message [0];} if (! mes.substring (3, 1) .Equals ("))) {return readline ();} if (debug) {for (int K = 0; k

转载请注明原文地址:https://www.9cbs.com/read-15255.html

New Post(0)