




免費預(yù)覽已結(jié)束,剩余16頁可下載查看
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
課程設(shè)計任務(wù)書專業(yè):計算機科學(xué)與技術(shù) 學(xué)號:學(xué)生姓名(簽名): 設(shè)計題目:vb實現(xiàn)ftp協(xié)議的基本功能一、設(shè)計實驗條件 綜合摟實驗室二、設(shè)計任務(wù)及要求1. ftp協(xié)議可以使本地或遠程的客戶和服務(wù)器之間通過tcp傳輸協(xié)議進行文件傳輸;2. 要求利用vb實現(xiàn)ftp協(xié)議的基本功能。3. 用winsock控件來實現(xiàn)與ftp服務(wù)器端建立連接,該程序?qū)崿F(xiàn)下述命令功能:get:取遠方的一個文件put:傳給遠方一個文件pwd:顯示服務(wù)器當前目錄dir:列出服務(wù)器當前目錄cd:改變遠方當前目錄quit:退出返回4. 用serv-u搭建ftp服務(wù)器,設(shè)置客戶端訪問的用戶名、密碼、可以訪問的服務(wù)器目錄,對服務(wù)器可訪問目錄文件的操作權(quán)限(可讀、可寫)。三、設(shè)計報告的內(nèi)容1. 設(shè)計題目與設(shè)計任務(wù)(設(shè)計任務(wù)書)設(shè)計題目:實現(xiàn)ftp協(xié)議的基本功能,實現(xiàn)客戶機和服務(wù)器之間文件的上傳、下載、修改、重名名等設(shè)計任務(wù):使用winsock控件,通過tcp協(xié)議與ftp服務(wù)器端建立連接。使用的環(huán)境是microsoft visual basic 6.0 中文版,ftp服務(wù)器搭建是基于serv-u軟件建立的服務(wù)器。2. 前言(緒論)(設(shè)計的目的、意義等)文件傳輸協(xié)議ftp(file transfer protocol)是在tcp/ip網(wǎng)絡(luò)中傳輸文件的一種格式規(guī)范,其目標是保證主機之間可靠和高效的傳輸程序或數(shù)據(jù),向用戶屏蔽不同主機中各種文件存儲系統(tǒng)的細節(jié)。其規(guī)定在用戶和服務(wù)器之間建立兩個并行的tcp連接:控制連接和數(shù)據(jù)連接。前者是建立在用戶協(xié)議解釋器和服務(wù)器協(xié)議解釋器之間用于交換命令與響應(yīng)的通信鏈路;后者是傳輸數(shù)據(jù)的全雙工連接。數(shù)據(jù)連接要先將文件拆分成分組再進行傳送,這樣做的目的是為了更加好的適應(yīng)網(wǎng)絡(luò)中的帶寬限制,以及減少傳送期間節(jié)點的延時,以達到高速傳送。文件傳送是各種計算機網(wǎng)絡(luò)都實現(xiàn)的基本功能,文件傳送協(xié)議是一種最基本的應(yīng)用層協(xié)議按照客戶/服務(wù)器的模式進行工作,提供交互式的訪問,是internet使用最廣泛的協(xié)議之一。本設(shè)計是用vb語言簡單實現(xiàn)文件傳輸協(xié)議,主要利用winsock控件,當中實現(xiàn)了上傳、下載、獲取服務(wù)器目錄等基本文件傳輸功能。 客戶端服務(wù)器數(shù)據(jù)傳輸控制字傳輸3. 設(shè)計主體(各部分設(shè)計內(nèi)容、分析、結(jié)論等)(1)建立基本的工程和窗口 實現(xiàn)“系統(tǒng)信息”功能。通過查詢注冊表來獲取一些相關(guān)的信息,導(dǎo)入要用到的訪問注冊版的api, 通過填寫服務(wù)器地址,用戶名和用戶密碼來實現(xiàn)與服務(wù)器的連接。可以匿名訪問也可以填寫用戶名訪問。 下圖:設(shè)計主窗口的菜單和其他一些必需的控件。如:工具欄、左邊的treeview控件、添加右邊的listview控件等。 服務(wù)器配置:設(shè)置用戶有關(guān)信息 (2)基本類模塊:用于處理相關(guān)鏈接、上傳、下載等操作 1實現(xiàn)ftp文件類,即把有關(guān)的信息封裝成一個類,實現(xiàn)面向?qū)ο蟮乃枷?,便于管理private mvarfilename as string private mvarlastwritetime as date private mvarfilesize as long private mvarisdirectory as booleanprivate mvarfilepath as stringpublic property let isdirectory(byval vdata as boolean) mvarisdirectory = vdataend propertypublic property get isdirectory() as boolean isdirectory = mvarisdirectoryend propertypublic property let filesize(byval vdata as long) mvarfilesize = vdataend propertypublic property get filesize() as long filesize = mvarfilesizeend propertypublic property let lastwritetime(byval vdata as date) mvarlastwritetime = vdataend propertypublic property get lastwritetime() as date lastwritetime = mvarlastwritetimeend propertypublic property let filename(byval vdata as string) mvarfilename = vdataend propertypublic property get filename() as string filename = mvarfilenameend propertypublic property let filepath(byval vdata as string) mvarfilepath = vdataend propertypublic property get filepath() as string filepath = mvarfilepathend property 2實現(xiàn)ftp連接類。定義兩個winsock控件變量,一個用戶控制連接,一個用于數(shù)據(jù)連接*winsock control for control connectionprivate withevents wsccontrol as mswinsocklib.winsockwinsock control for data connectionprivate withevents wscdata as mswinsocklib.winsock*為wsccontrol添加dataarrival事件響應(yīng)方法。通過調(diào)用getdata方法獲取到達的數(shù)據(jù),并加在原來的數(shù)據(jù)的后面,如果相應(yīng)碼是426,表示與服務(wù)器的連接關(guān)閉,重置類變量,并關(guān)閉wsccontrol。private sub wsccontrol_dataarrival(byval bytestotal as long) dim strdata as string wsccontrol.getdata strdata m_strwinsockbuffer = m_strwinsockbuffer & strdata m_strlastserverresponse = strdata m_objtimeout.reset if getresponsecode(strdata) = 426 then if m_btransferinprogress or m_buploadfile then wscdata.close close m_intlocalfileid m_strdatabuffer = m_ldownloadedbytes = 0 m_luploadedbytes = 0 m_btransferinprogress = false m_buploadfile = false m_bfileisopened = false end if wsccontrol.close m_bbusy = false end if debug.print left(strdata, len(strdata) - 2)end sub處理與ftp服務(wù)器的連接。用的ftp服務(wù)器需要用戶名和密碼進行帳戶認證,需要先處理user命令和pass命令,獲取用戶名,附加到user命令中發(fā)送到服務(wù)器。并處理服務(wù)器的響應(yīng),最后返回ftp相應(yīng)碼,標識登錄是否成功。private function processusercommand() as ftp_response_codes dim strdata as string on error goto processusercommand_err_handler raiseevent statechanged(ftp_connection_authentication) m_strusername = iif(len(m_strusername) 0, m_strusername, anonymous) if len(m_strpassword) = 0 then if m_strusername = anonymous then m_strpassword = else raise error exit function end if end if wsccontrol.senddata user & m_strusername & vbcrlf debug.print user & m_strusername m_objtimeout.starttimer do doevents if m_objtimeout.timeout then m_lasterror = error_ftp_user_timeout exit do end if if len(m_strwinsockbuffer) response_code_lenght then strdata = m_strwinsockbuffer m_strwinsockbuffer = exit do end if loop m_objtimeout.stoptimer select case getresponsecode(strdata) case ftp_response_user_logged_in processusercommand = ftp_response_user_logged_in case ftp_response_user_name_ok_need_password processusercommand = ftp_response_user_name_ok_need_password case else processftpresponse getresponsecode(strdata) end select exit_label: exit functionprocessusercommand_err_handler: if not processwinsockerror(err.number, err.description) then err.raise vbobjecterror + 1000 + err.number, cftpconnection.processusercommand, err.description end if goto exit_label end functionprivate function processpasscommand() as ftp_response_codes dim strresponse as string dim strdata as string on error goto processpasscommand_err_handler wsccontrol.senddata pass & m_strpassword & vbcrlf debug.print pass & m_strpassword m_objtimeout.starttimer do doevents if m_objtimeout.timeout then m_lasterror = error_ftp_user_timeout exit do end if if len(m_strwinsockbuffer) response_code_lenght then strdata = m_strwinsockbuffer exit do end if loop m_objtimeout.stoptimer if getresponsecode(strdata) = ftp_response_user_logged_in then do doevents if instr(1, m_strwinsockbuffer, 230 ) 0 then processpasscommand = ftp_response_user_logged_in m_strwinsockbuffer = exit function end if loop else processftpresponse getresponsecode(strdata) end if processpasscommand = getresponsecode(strdata) exit_label: exit functionprocesspasscommand_err_handler: if not processwinsockerror(err.number, err.description) then err.raise vbobjecterror + 1000 + err.number, cftpconnection.processpasscommand, err.description end if goto exit_label end function 下面方法附加到pass命令中發(fā)送到服務(wù)器,并處理服務(wù)器的響應(yīng)。private function processpwdcommand() as boolean dim strresponse as string dim strdata as string on error goto processpwdcommand_err_handler wsccontrol.senddata pwd & vbcrlf debug.print pwd m_objtimeout.starttimer do doevents if m_objtimeout.timeout then m_lasterror = error_ftp_user_timeout exit do end if if instr(1, m_strwinsockbuffer, vbcrlf) 0 then strdata = m_strwinsockbuffer m_strwinsockbuffer = exit do end if loop m_objtimeout.stoptimer if getresponsecode(strdata) = ftp_response_pathname_created then dim intposa as integer, intposb as integer intposa = instr(1, strdata, chr$(34) + 1 intposb = instr(intposa, strdata, chr$(34) if intposa 1 and intposb 0 then m_strcurrentdirectory = mid$(strdata, intposa, intposb - intposa) processpwdcommand = true else raise error - unknown response format end if else processftpresponse getresponsecode(strdata) end if exit_label: exit functionprocesspwdcommand_err_handler: if not processwinsockerror(err.number, err.description) then err.raise vbobjecterror + 1000 + err.number, cftpconnection.processpwdcommand, err.description end if goto exit_label end functionprivate sub class_terminate() call breakeconnection set wscdata = nothing set wsccontrol = nothing m_objtimeout.stoptimer set m_objtimeout = nothing end subprivate sub wsccontrol_dataarrival(byval bytestotal as long) dim strdata as string wsccontrol.getdata strdata m_strwinsockbuffer = m_strwinsockbuffer & strdata m_strlastserverresponse = strdata m_objtimeout.reset if getresponsecode(strdata) = 426 then if m_btransferinprogress or m_buploadfile then wscdata.close close m_intlocalfileid m_strdatabuffer = m_ldownloadedbytes = 0 m_luploadedbytes = 0 m_btransferinprogress = false m_buploadfile = false m_bfileisopened = false end if wsccontrol.close m_bbusy = false end if debug.print left(strdata, len(strdata) - 2) end subprivate function processportcommand() as boolean dim intport as integer dim stripaddress as string dim colipaddresses as new collection dim strsend as string dim strdata as string on error resume next raiseevent statechanged(ftp_establishing_data_connection) do intport = getfreeport if wscdata.state sckclosed then wscdata.close wscdata.localport = intport wscdata.listen if not err then exit do loop on error goto processportcommand_err_handler stripaddress = cstr(wsccontrol.localip) strsend = port & replace(stripaddress, ., ,) strsend = strsend & , & intport 256 & , & (intport mod 256) strsend = strsend & vbcrlf wsccontrol.senddata strsend debug.print left(strsend, len(strsend) - 2) m_objtimeout.starttimer do doevents if m_objtimeout.timeout then m_lasterror = error_ftp_user_timeout exit do end if if instr(1, m_strwinsockbuffer, vbcrlf) 0 then strdata = m_strwinsockbuffer m_strwinsockbuffer = exit do end if loop m_objtimeout.stoptimer if getresponsecode(strdata) = ftp_response_command_ok then processportcommand = true raiseevent statechanged(ftp_data_connection_established) else processftpresponse getresponsecode(strdata) end if exit_label: exit functionprocessportcommand_err_handler: if not processwinsockerror(err.number, err.description) then err.raise vbobjecterror + 1000 + err.number, cftpconnection.processportcommand, err.description end if goto exit_label end function在和服務(wù)器連接之前,首先要保證服務(wù)器的地址不為空,關(guān)閉wsccontrol將連接設(shè)為0,連接服務(wù)器。啟動定時器,如果超時,就退出,否則檢查wsccontrol的狀態(tài),并進行相應(yīng)的處理。public function connect() as booleanon error goto connect_err_handlerdim strdata as stringm_strwinsockbuffer = m_bbusy = trueif len(m_varftpserver) 0 then with wsccontrol .close .localport = 0 .connect m_varftpserver, 21 m_objtimeout.starttimer do doevents if m_objtimeout.timeout then m_lasterror = error_ftp_user_timeout exit do end if if .state = sckconnected then m_objtimeout.stoptimer raiseevent statechanged(ftp_connection_connected) m_objtimeout.starttimer do doevents if m_objtimeout.timeout then m_lasterror = error_ftp_user_timeout exit do end if if len(m_strwinsockbuffer) (response_code_lenght - 1) then strdata = m_strwinsockbuffer m_strwinsockbuffer = exit do end if loop m_objtimeout.stoptimer select case getresponsecode(strdata) case ftp_response_service_ready_for_new_user select case processusercommand case ftp_response_user_logged_in connect = true case ftp_response_user_name_ok_need_password if processpasscommand = ftp_response_user_logged_in then connect = true end if end select get working directory if connect then call processpwdcommand end if case ftp_response_service_ready_in_minutes 120 service ready in nnn minutes. m_lasterror = error_ftp_protocol_service_ready_in_minutes case ftp_response_service_not_available_closing_control_connection 421 service not available, closing control connection. m_lasterror = error_ftp_protocol_service_not_available_closing_control_connection end select exit do elseif .state = sckconnectaborted then m_lasterror = error_ftp_winsock_connectaborted elseif .state = sckresolvinghost then raiseevent statechanged(ftp_connection_resolving_host) elseif .state = sckhostresolved then raiseevent statechanged(ftp_connection_host_resolved) end if loop m_objtimeout.stoptimer end withelse raise error connect = false exit functionend ifexit_label: if connect then raiseevent statechanged(ftp_user_logged) m_bbusy = false exit functionconnect_err_handler: if not processwinsockerror(err.number, err.description) then err.raise vbobjecterror + 1000 + err.number, cftpconnection.connect, err.description end if goto exit_label end function下載文件:文件傳輸涉及到重新開始命令和獲得文件命令。重新開始命令的命令參數(shù)域代表服務(wù)器要重新開始的那一點,此命令并不傳送文件,而是簡單指定點后的數(shù)據(jù),獲得文件命令使服務(wù)器dtp傳送指定路徑內(nèi)的文件副本到服務(wù)器或用戶dtp,這邊服務(wù)器上文件的狀態(tài)和內(nèi)容不受影響。processrestcommand方法向ftp服務(wù)器發(fā)送rest命令,告訴服務(wù)器文件要開始傳輸?shù)奈恢?,并處理服?wù)器的響應(yīng)。如果成功返回true,反之返回false。private function processrestcommand(lstartpoint as long) as boolean dim strresponse as string dim strdata as string on error goto processrestcommand_err_handler wsccontrol.senddata rest & lstartpoint & vbcrlf debug.print rest & lstartpoint m_objtimeout.starttimer do doevents if m_objtimeout.timeout then m_lasterror = error_ftp_user_timeout exit do end if if instr(1, m_strwinsockbuffer, vbcrlf) 0 then strdata = m_strwinsockbuffer m_strwinsockbuffer = exit do end if loop m_objtimeout.stoptimer if getresponsecode(strdata) = ftp_response_requested_file_action_pending_further_info then processrestcommand = true else processftpresponse getresponsecode(strdata) end if exit_label: exit functionprocessrestcommand_err_handler: if not processwinsockerror(err.number, err.description) then err.raise vbobjecterror + 1000 + err.number, cftpconnection.processrestcommand, err.description end if goto exit_label end functionprocessretrcommand方法向ftp服務(wù)器發(fā)送rest命令,讓ftp服務(wù)器傳送指定路徑的文件副本到本地,并處理服務(wù)器的響應(yīng)。如果成功返回true,反之返回false。private function processretrcommand(strfilename as string, lstartpoint as long) as boolean dim strresponse as string dim strdata as string on error goto processretrcommand_err_handler m_strdatabuffer = wsccontrol.senddata retr & strfilename & vbcrlf debug.print retr & strfilename m_objtimeout.starttimer do doevents if m_objtimeout.timeout then m_lasterror = error_ftp_user_timeout exit do end if if not m_btransferinprogress then strdata = m_strwinsockbuffer exit do end if if instr(1, m_strwinsockbuffer, vbcrlf) 0 then if getresponsecode(m_strwinsockbuffer) = 150 or _ getresponsecode(m_strwinsockbuffer) = 125 then if lstartpoint = 0 and fileexists(m_strlocalfilepath) then kill m_strlocalfilepath end if m_intlocalfileid = freefile open m_strlocalfilepath for binary as m_intlocalfileid if lstartpoint 0 then seek m_intlocalfileid, lstartpoint + 1 end if turn on flag m_bfileisopened m_bfileisopened = true ignore 150 and 125 reply codes m_strwinsockbuffer = mid$(m_strwinsockbuffer, instr(1, m_strwinsockbuffer, vbcrlf) + 2) raiseevent statechanged(ftp_transfer_starting) else strdata = m_strwinsockbuffe
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 安全知識法試題及答案
- 2025年電動汽車電池熱管理系統(tǒng)熱管理效率優(yōu)化與創(chuàng)新研究報告
- 安全技能比武試題及答案
- 安全工作教育試題及答案
- 物業(yè)品質(zhì)培訓(xùn)課件目錄
- 魔鏡檢測皮膚培訓(xùn)課件
- 重疾保險培訓(xùn)課件
- 《編制說明蒙農(nóng)1號蒙古冰草提純復(fù)壯技術(shù)規(guī)程》
- 中班家園共育課件
- 冬季生產(chǎn)安全培訓(xùn)
- 生物高中-基于大數(shù)據(jù)分析的精準教學(xué)課件
- 工程結(jié)算審計實施方案(共8篇)
- 樂東221氣田投產(chǎn)專家驗收匯報
- 信任五環(huán)(用友營銷技巧)課件
- 2022年廣東省深圳市中考化學(xué)真題試卷
- 危險貨物道路運輸安全生產(chǎn)管理制度
- GB∕T 8110-2020 熔化極氣體保護電弧焊用非合金鋼及細晶粒鋼實心焊絲
- 【完美排版】山東科技出版社二年級下冊綜合實踐活動教案
- 制造業(yè)成本核算表格(有自動計算的公式)
- 公共政策學(xué)(第三版)-課件
- 齊魯醫(yī)學(xué)Lisfranc-損傷
評論
0/150
提交評論