計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)利用VB實(shí)現(xiàn)FTP協(xié)議的基本功能_第1頁(yè)
計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)利用VB實(shí)現(xiàn)FTP協(xié)議的基本功能_第2頁(yè)
計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)利用VB實(shí)現(xiàn)FTP協(xié)議的基本功能_第3頁(yè)
計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)利用VB實(shí)現(xiàn)FTP協(xié)議的基本功能_第4頁(yè)
計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)利用VB實(shí)現(xiàn)FTP協(xié)議的基本功能_第5頁(yè)
已閱讀5頁(yè),還剩16頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、課程設(shè)計(jì)任務(wù)書專業(yè):計(jì)算機(jī)科學(xué)與技術(shù) 學(xué)號(hào):學(xué)生姓名(簽名): 設(shè)計(jì)題目:vb實(shí)現(xiàn)ftp協(xié)議的基本功能一、設(shè)計(jì)實(shí)驗(yàn)條件 綜合摟實(shí)驗(yàn)室二、設(shè)計(jì)任務(wù)及要求1. ftp協(xié)議可以使本地或遠(yuǎn)程的客戶和服務(wù)器之間通過tcp傳輸協(xié)議進(jìn)行文件傳輸;2. 要求利用vb實(shí)現(xiàn)ftp協(xié)議的基本功能。3. 用winsock控件來實(shí)現(xiàn)與ftp服務(wù)器端建立連接,該程序?qū)崿F(xiàn)下述命令功能:get:取遠(yuǎn)方的一個(gè)文件put:傳給遠(yuǎn)方一個(gè)文件pwd:顯示服務(wù)器當(dāng)前目錄dir:列出服務(wù)器當(dāng)前目錄cd:改變遠(yuǎn)方當(dāng)前目錄quit:退出返回4. 用serv-u搭建ftp服務(wù)器,設(shè)置客戶端訪問的用戶名、密碼、可以訪問的服務(wù)器目錄,對(duì)服務(wù)器可訪

2、問目錄文件的操作權(quán)限(可讀、可寫)。三、設(shè)計(jì)報(bào)告的內(nèi)容1. 設(shè)計(jì)題目與設(shè)計(jì)任務(wù)(設(shè)計(jì)任務(wù)書)設(shè)計(jì)題目:實(shí)現(xiàn)ftp協(xié)議的基本功能,實(shí)現(xiàn)客戶機(jī)和服務(wù)器之間文件的上傳、下載、修改、重名名等設(shè)計(jì)任務(wù):使用winsock控件,通過tcp協(xié)議與ftp服務(wù)器端建立連接。使用的環(huán)境是microsoft visual basic 6.0 中文版,ftp服務(wù)器搭建是基于serv-u軟件建立的服務(wù)器。2. 前言(緒論)(設(shè)計(jì)的目的、意義等)文件傳輸協(xié)議ftp(file transfer protocol)是在tcp/ip網(wǎng)絡(luò)中傳輸文件的一種格式規(guī)范,其目標(biāo)是保證主機(jī)之間可靠和高效的傳輸程序或數(shù)據(jù),向用戶屏蔽不同主機(jī)

3、中各種文件存儲(chǔ)系統(tǒng)的細(xì)節(jié)。其規(guī)定在用戶和服務(wù)器之間建立兩個(gè)并行的tcp連接:控制連接和數(shù)據(jù)連接。前者是建立在用戶協(xié)議解釋器和服務(wù)器協(xié)議解釋器之間用于交換命令與響應(yīng)的通信鏈路;后者是傳輸數(shù)據(jù)的全雙工連接。數(shù)據(jù)連接要先將文件拆分成分組再進(jìn)行傳送,這樣做的目的是為了更加好的適應(yīng)網(wǎng)絡(luò)中的帶寬限制,以及減少傳送期間節(jié)點(diǎn)的延時(shí),以達(dá)到高速傳送。文件傳送是各種計(jì)算機(jī)網(wǎng)絡(luò)都實(shí)現(xiàn)的基本功能,文件傳送協(xié)議是一種最基本的應(yīng)用層協(xié)議按照客戶/服務(wù)器的模式進(jìn)行工作,提供交互式的訪問,是internet使用最廣泛的協(xié)議之一。本設(shè)計(jì)是用vb語(yǔ)言簡(jiǎn)單實(shí)現(xiàn)文件傳輸協(xié)議,主要利用winsock控件,當(dāng)中實(shí)現(xiàn)了上傳、下載、獲取服務(wù)

4、器目錄等基本文件傳輸功能。 客戶端服務(wù)器數(shù)據(jù)傳輸控制字傳輸3. 設(shè)計(jì)主體(各部分設(shè)計(jì)內(nèi)容、分析、結(jié)論等)(1)建立基本的工程和窗口 實(shí)現(xiàn)“系統(tǒng)信息”功能。通過查詢注冊(cè)表來獲取一些相關(guān)的信息,導(dǎo)入要用到的訪問注冊(cè)版的api, 通過填寫服務(wù)器地址,用戶名和用戶密碼來實(shí)現(xiàn)與服務(wù)器的連接??梢阅涿L問也可以填寫用戶名訪問。 下圖:設(shè)計(jì)主窗口的菜單和其他一些必需的控件。如:工具欄、左邊的treeview控件、添加右邊的listview控件等。 服務(wù)器配置:設(shè)置用戶有關(guān)信息 (2)基本類模塊:用于處理相關(guān)鏈接、上傳、下載等操作 1實(shí)現(xiàn)ftp文件類,即把有關(guān)的信息封裝成一個(gè)類,實(shí)現(xiàn)面向?qū)ο蟮乃枷耄阌诠芾韕

5、rivate 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 isdirec

6、tory = 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 ge

7、t 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 = vd

8、ataend propertypublic property get filepath() as string filepath = mvarfilepathend property 2實(shí)現(xiàn)ftp連接類。定義兩個(gè)winsock控件變量,一個(gè)用戶控制連接,一個(gè)用于數(shù)據(jù)連接*winsock control for control connectionprivate withevents wsccontrol as mswinsocklib.winsockwinsock control for data connectionprivate withevents wscdata as mswinsoc

9、klib.winsock*為wsccontrol添加dataarrival事件響應(yīng)方法。通過調(diào)用getdata方法獲取到達(dá)的數(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_strlastserverrespon

10、se = 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

11、wsccontrol.close m_bbusy = false end if debug.print left(strdata, len(strdata) - 2)end sub處理與ftp服務(wù)器的連接。用的ftp服務(wù)器需要用戶名和密碼進(jìn)行帳戶認(rèn)證,需要先處理user命令和pass命令,獲取用戶名,附加到user命令中發(fā)送到服務(wù)器。并處理服務(wù)器的響應(yīng),最后返回ftp相應(yīng)碼,標(biāo)識(shí)登錄是否成功。private function processusercommand() as ftp_response_codes dim strdata as string on error goto proces

12、susercommand_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 = guest else raise error exit function end if end if wsccontrol.senddata

13、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 lo

14、op 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(strd

15、ata) 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

16、() 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_timeo

17、ut 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_use

18、r_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 vbobjecterr

19、or + 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.senddat

20、a 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 getrespon

21、secode(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 = tr

22、ue 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.processpwdcomm

23、and, 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 wscco

24、ntrol.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_luploaded

25、bytes = 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 colipaddresse

26、s 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

27、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 d

28、o 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 processpo

29、rtcommand = 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, cftpcon

30、nection.processportcommand, err.description end if goto exit_label end function在和服務(wù)器連接之前,首先要保證服務(wù)器的地址不為空,關(guān)閉wsccontrol將連接設(shè)為0,連接服務(wù)器。啟動(dòng)定時(shí)器,如果超時(shí),就退出,否則檢查wsccontrol的狀態(tài),并進(jìn)行相應(yīng)的處理。public function connect() as booleanon error goto connect_err_handlerdim strdata as stringm_strwinsockbuffer = m_bbusy = trueif l

31、en(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

32、_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 getresp

33、onsecode(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 i

34、f 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

35、. 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 = sc

36、khostresolved 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 proces

37、swinsockerror(err.number, err.description) then err.raise vbobjecterror + 1000 + err.number, cftpconnection.connect, err.description end if goto exit_label end function下載文件:文件傳輸涉及到重新開始命令和獲得文件命令。重新開始命令的命令參數(shù)域代表服務(wù)器要重新開始的那一點(diǎn),此命令并不傳送文件,而是簡(jiǎn)單指定點(diǎn)后的數(shù)據(jù),獲得文件命令使服務(wù)器dtp傳送指定路徑內(nèi)的文件副本到服務(wù)器或用戶dtp,這邊服務(wù)器上文件的狀態(tài)和內(nèi)容不受影響。pr

38、ocessrestcommand方法向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.prin

39、t 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(str

40、data) = 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

41、+ 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 s

42、tring 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_btra

43、nsferinprogress 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

44、_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_strwinsockbuffer

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論