Network Programming-FTP client implementation (C)

Source: Internet
Author: User
Tags baseuri ftp client
. Net2.0 has a good encapsulation of FTP, but it is easy to ignore the real internal implementation of FTP. The following is the function of the FTP client I implemented. The main steps are as follows:
1. Create an ftpwebrequest object pointing to the FTP server URI
2. Set the FTP execution method (upload, download, etc)
3. Set attributes for the ftpwebrequest object (whether to support SSL or binary transmission)
4. Set logon authentication (user name and password)
5. execute the request
6. Receive the corresponding stream (if needed)
7. If no stream is opened, disable the FTP request.

Develop any FTP application Program All require an FTP server and its configuration information. Ftpwebrequest exposes some attributes to set this information.
First, connect and retrieve all the directories and file lists in FTP. Cursor currentcursor =   This . Cursor;
Ftpwebresponse respons =   Null ;
Stream stream =   Null ;
Try
{
This . Cursor = Cursors. waitcursor;
Ftpwebrequest request = (Ftpwebrequest) webrequest. Create (textbox1.text );
Request. Credentials =   New Networkcredential (textname. Text, textpwd. Text );
Request. Method = Webrequestmethods. FTP. listdirectory;
Respons = (Ftpwebresponse) request. getresponse ();
Stream = Respons. getresponsestream ();
Filldirectorylist (Stream );
Serverdirecf.pdf =   Null ;

}
Catch (Exception ex)
{
MessageBox. Show (ex. Message,"Error FTP Client", Messageboxbuttons. OK, messageboxicon. Error );

}
Finally
{
If (Respons ! =   Null )
Respons. Close ();
If (Stream ! =   Null )
Stream. Close ();
This . Cursor = Currentcursor;
}

}

Open a directory: Ftpwebresponse response =   Null ;
Stream stream =   Null ;
Try
{
String Subdir = Listbox1.selectedvalue. tostring (). Trim ();
Serverdirecf.pdf + =   " / "   + Subdir;
Uri baseuri =   New Uri (textbox1.text );
Uri URI =   New Uri (baseuri, serverdirecf.pdf );
Ftpwebrequest request = (Ftpwebrequest) webrequest. Create (URI );
Request. Credentials =   New Networkcredential (textname. Text, textpwd. Text );
Request. Method = Webrequestmethods. FTP. listdirectory;
Response = (Ftpwebresponse) request. getresponse (); stream = Response. getresponsestream ();
Filldirectorylist (Stream );
// Serverdirecf.pdf = NULL;

}
Catch (Exception ex)
{
MessageBox. Show (ex. Message,"Error FTP Client", Messageboxbuttons. OK, messageboxicon. Error );

}
Finally
{
If (Response ! =   Null )
Response. Close ();
If (Stream ! =   Null )
Stream. Close ();
}

Upload function Private   Void Upload ( String Filename)
{
Fileinfo fileinf =   New Fileinfo (filename );
String Uri =   " FTP :// "   + Ftpserverip +   " / "   + Fileinf. Name;
Ftpwebrequest reqftp;

// Create an ftpwebrequest object based on Uri
Reqftp = (Ftpwebrequest) ftpwebrequest. Create ( New Uri ( " FTP :// "   + Ftpserverip +   " / "   + Fileinf. Name ));

// FTP user name and password
Reqftp. Credentials =   New Networkcredential (ftpuserid, ftppassword );

// The default value is true, and the connection will not be closed.
// Executed after a command
Reqftp. keepalive =   False ;

// Specify the command to be executed
Reqftp. Method = Webrequestmethods. FTP. uploadfile;

// Data transmission type
Reqftp. usebinary =   True ;

// Notify the server of file size when uploading files
Reqftp. contentlength = Fileinf. length;

// Set the buffer size to 2 kb.
Int Bufflength =   2048 ;

Byte [] Buff =   New   Byte [Bufflength];
Int Contentlen;

// Open a file stream (system. Io. filestream) to read the uploaded file.
Filestream FS = Fileinf. openread ();
Try  
{
// Write uploaded files to the stream
Stream STRM = Reqftp. getrequeststream ();

// 2 kb of each file stream read
Contentlen = FS. Read (buff, 0 , Bufflength );

// The stream content has not ended
While (Contentlen ! =   0 )
{
//Write content from file stream to upload stream
STRM. Write (buff,0, Contentlen );

Contentlen=FS. Read (buff,0, Bufflength );
}  

// Close two streams
STRM. Close ();
FS. Close ();
}  
Catch (Exception ex)
{
MessageBox. Show (ex. Message,"Upload Error");
}  
}  

This may be a bit clear. We found that, whether it is display, upload, or transfer to the lower-level, their methods are consistent, that is, the steps we mentioned at the beginning, the focus is to set the current path, here I used a global variable string serverdirecf.pdf for storage. At that time, we set the root directory to null. When the directory changes, we can change its value, for exampleServerdirecf.pdf + = "/" + subdir;At the same time, it is critical to set the command to be executed:
Request. method = webrequestmethods. FTP. listdirectory (rename, delete, getfilesize, filelistdetails, makedir); then we can implement the entire FTP client function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.