如何用vc++編制ftp客戶端應用程式
ftp協議將使用兩條單獨的tcp連線,一條專用於傳送ftp命令,另一條則專用於傳遞資料。初始建立連線時,伺服器在21號埠上接收來自客戶端的命令連線。當需要傳送資料時(檔案列表、檔案資料等),客戶端向伺服器發出port命令,並進入監聽狀態,等待來自伺服器的資料連線請求。
■ccommandsocket類的主要**
void ccommandsocket::onreceive(int nerrorcode)
//這個函式使得伺服器的應答訊息顯示在編輯框上
char buffer=new char[4096];
memset(buffer,0,4096);
this-〉receive(buffer,1024,0);
//接收應答訊息
messagelist+=buffer;
m_returnmessage-〉setwindowtext(messagelist);
delete buffer;
■cfilesocket類的主要**
void cfilesocket::onreceive(int nerrorcode)
//函式將收到的檔案資料寫到檔案中
if(file= =null)
{ file=new cfile();
file-〉open(filename,cfile::modewrite|cfile::modecreate);
charbuffer=new char[4096];
memset(buffer,0,4096);
this-〉receive(buffer,4096,0);
receivestring=buffer;
file-〉write(receivestring,receivestring.getlength( ));
delete buffer;
■creceivesocket類的主要**
void creceivesocket::onreceive(int nerrorcode)
//接收伺服器傳來的檔案列表訊息
cstring receivestring,temp;
charbuffer=new char[4096];
memset(buffer,0,4096);
this-〉receive(buffer,4096,0); //接收訊息
receivestring+=buffer;
delete buffer;
//將檔案列表從收到的訊息字串中分離出來,並顯示在列表框中
while(!receivestring.isempty())
{ int p=receivestring.find("/r/n");
if(p!=-1)
{ temp=receivestring.left(p);
receivestring=receivestring.right(receivestring.getlength()-p-2);
displaymessage-〉addstring(temp);
■cportsocket類主要**
void cportsocket::onaccept(int nerrorcode)
//根據不同的標誌選擇相應的資料連線類,以接受伺服器端的資料連線請求
if(flag= =listfile)
//若程式要求對目錄進行列表,則採用creceivesocket類
{datasocket=new creceivesocket(filelist);
this-〉accept(datasocket);
else if(flag= =download)
{filesocket=new cfilesocket(filename);
this-〉accept(filesocket);
■主對話方塊類cftpclient- demodlg的主要**
void cftpclientdemodlg::onfilelist()
//響應「檔案列表」按鈕、列表目錄
{ cstring temp;
if(controlsocket= =null)
//連線到ftp伺服器
controlsocket=new ccommandsocket(&&m_returnmessage);
controlsocket-〉create();
m_server.getwindowtext(temp);
controlsocket-〉connect(temp,21);
//ftp伺服器在21號埠接收連線
m_user.getwindowtext(temp);
temp="user "+temp+"/r/n";
controlsocket-〉send(temp,temp.getlength(),0);
//發user命令,驗證使用者
m_pass.getwindowtext(temp); //m_pass為「口令」編輯框的對應控制
temp="pass "+temp+"/r/n";
controlsocket-〉send(temp,temp.getlength(),0);
//發pass命令,校驗口令
lisentport(listfile);
//資料連線的物件為目錄列表
controlsocket-〉send("list /r/n",7 ,0);
//發list命令,要求列表目錄
void cftpclientdemodlg::ondownload( )
cstring string;
lisentport(download);
//獲得要下戴檔案的檔名
m_localfile.getwindowtext(string);
// m_localfile為「檔名」編輯框的對應控制
string="retr "+string+"/r/n";
controlsocket-〉send(string,string.getlength( ),0);
void cftpclientdemodlg::lisentport(uint flag)
//根據要求選擇不同的資料連線物件
if(lisentsocket!=null)
//清空lisentsocket
{ lisentsocket-〉close();
delete lisentsocket;
lisentsocket=null;
if(flag= =listfile)
//如果為目錄列表資料連線物件
{ lisentsocket=new cportsocket(listfile);
lisentsocket-〉setlistbox(&&m_filelist);
//傳列表框到clisentsocket類中
else if(flag= =download)
//如果為檔案傳輸資料連線物件
{ cstring string;
m_localfile.getwindowtext(string);
lisentsocket=new cportsocket(download);
lisentsocket-〉setfilename(string);
//傳檔名到clisentsocket類中
lisentsocket-〉create();
//建立socket並進行監聽,等待ftp伺服器進行資料連線
lisentsocket-〉listen();
//取得資料連線socket的ip位址和監聽埠,並把它們作為port命令的引數
sockaddr_in listing_address, control_address;
int addr_size;
addr_size = sizeof(listing_address);
lisentsocket-〉getsockname((sockaddr )&&listing_address, &&addr_size); //取ip位址
controlsocket-〉getsockname((sockaddr )&&control_address, &&addr_size); //取埠
unsigned char port = (unsigned char )&&(listing_address.sin_port);
unsigned char host = (unsigned char )&&(control_address.sin_addr);
cstring strbuffer;
strbuffer.format("port %i,%i,%i,%i,%i,%i/r/n",(int)host[0], (int)host[1], (int)host[2], (int)host[3],(int)port[0], (int)port[1]);
controlsocket-〉send(strbuffer,strbuffer.getlength(),0);
//傳送port命令,進行資料連線
以上**在vc++ 6.0、windows 98上執行通過。
FTP客戶端程式
ftp客戶端程式,vc6.0下除錯通過 client.c include client.h define sol socket 0xffff int fill host addr char host ip addr,struct sockaddr in host,int port else retu...
ftp客戶端程式
include include include include include pragma comment lib,ws2 32.lib file fp socket client,upload wsadata wsadata int wsareturn int portnum char spor...
Python編寫FTP客戶端
之前寫過一篇ftp服務端的文章,這篇介紹一下客戶端吧。在使用虛擬機器的時候,由於虛擬機器工具沒安裝成功,所以我決定用ftp在主機與虛擬機器之間傳送檔案,在虛擬機器上開啟ftp服務,然後把客戶端放在主機上,當然也可以顛倒過來。服務端請參考 python實現ftp伺服器 import ftplib im...