116.網(wǎng) 絡(luò) 程 序 設(shè) 計報告_第1頁
116.網(wǎng) 絡(luò) 程 序 設(shè) 計報告_第2頁
116.網(wǎng) 絡(luò) 程 序 設(shè) 計報告_第3頁
116.網(wǎng) 絡(luò) 程 序 設(shè) 計報告_第4頁
116.網(wǎng) 絡(luò) 程 序 設(shè) 計報告_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、設(shè)計報告課程名稱: 網(wǎng) 絡(luò) 程 序 設(shè) 計 學 院:電子科學與信息技術(shù) 專 業(yè): 信 息 安 全 姓 名: 學 號: 年 級: 2005級 任課教師: 2008年 6月 29日tcpfiletransferdemo設(shè)計一、 設(shè)計目標:基于tcp的文件傳輸二、 技術(shù)原理簡介:應(yīng)用層網(wǎng)絡(luò)協(xié)議設(shè)計掌握tcp服務(wù)端和客戶端程序的編寫方法三、 需求分析:使用tcp監(jiān)聽的服務(wù)端網(wǎng)絡(luò)程序模型協(xié)議的解析處理文件列表獲取、傳輸和顯示功能的實現(xiàn)文件內(nèi)容獲取、傳輸和顯示功能的實現(xiàn) 四、 設(shè)計思路: 1、設(shè)計一個發(fā)送方windows窗體,上邊放目標ip、域名按鈕、一個端瀏覽鈕,開始,取消按鈕。2、一個tcp文件傳輸窗

2、體,啟動發(fā)送方按鈕,啟動接收方按鈕。3、接收方窗體,監(jiān)聽端口,開始按鈕,停止按鈕。五、設(shè)計框圖: 進行傳輸建立傳輸斷六、關(guān)鍵部分窗體:七、:關(guān)鍵代碼及其注釋:主窗體:private void initializecomponent() this.btnstartsender = new system.windows.forms.button(); this.startreceiver = new system.windows.forms.button(); this.dlgfindfile = new system.windows.forms.openfiledialog(); this.su

3、spendlayout(); / / startsender / this.btnstartsender.location = new system.drawing.point(16, 16); this.btnstartsender.name = "btnstartsender" this.btnstartsender.size = new system.drawing.size(176, 32); this.btnstartsender.tabindex = 0; this.btnstartsender.text = "啟動發(fā)送方" this.btn

4、startsender.click += new system.eventhandler(this.btnstartsender_click); / / startreceiver / this.startreceiver.location = new system.drawing.point(16, 56); this.startreceiver.name = "startreceiver" this.startreceiver.size = new system.drawing.size(176, 32); this.startreceiver.tabindex = 1

5、; this.startreceiver.text = "啟動接收方" this.startreceiver.click += new system.eventhandler(this.startreceiver_click); / / controller / this.autoscalebasesize = new system.drawing.size(6, 14); this.clientsize = new system.drawing.size(208, 104); this.controls.add(this.startreceiver); this.cont

6、rols.add(this.btnstartsender); this.formborderstyle = system.windows.forms.formborderstyle.fixedsingle; this.maximizebox = false; this.minimizebox = false; this.name = "controller" this.startposition = system.windows.forms.formstartposition.centerscreen; this.text = "tcp 文件傳輸" th

7、is.load += new system.eventhandler(this.controller_load); this.resumelayout(false);接收方:namespace receiverpublic class receiver : system.windows.forms.formprivate system.windows.forms.textbox txtport;private system.windows.forms.label label2;private system.windows.forms.button btnstart;private system

8、.windows.forms.label lblstatus;private system.windows.forms.label label4;private system.windows.forms.label label1;private system.windows.forms.button btnstop;private system.windows.forms.savefiledialog dlgsavefile;/ <summary>/ 必需的設(shè)計器變量。/ </summary>private system.componentmodel.container

9、 components = null;public receiver()initializecomponent();setstatusdelegate = new setstatusdelegate(setstatus);getsavefiledelegate = new getsavefiledelegate(getsavefile);/ 清理所有正在使用的資源。/ </summary>protected override void dispose( bool disposing )if( disposing )if (components != null) components

10、.dispose();base.dispose( disposing );#region windows 窗體設(shè)計器生成的代碼/ <summary>/ 設(shè)計器支持所需的方法- 不要使用代碼編輯器修改/ 此方法的內(nèi)容。/ </summary>private void initializecomponent() this.txtport = new system.windows.forms.textbox(); this.label2 = new system.windows.forms.label(); this.btnstart = new system.windows

11、.forms.button(); this.lblstatus = new system.windows.forms.label(); this.label4 = new system.windows.forms.label(); this.label1 = new system.windows.forms.label(); this.btnstop = new system.windows.forms.button(); this.dlgsavefile = new system.windows.forms.savefiledialog(); this.suspendlayout(); /

12、/ txtport / this.txtport.location = new system.drawing.point(80, 16); this.txtport.name = "txtport" this.txtport.size = new system.drawing.size(48, 21); this.txtport.tabindex = 1; this.txtport.text = "8080" / / label2 / this.label2.location = new system.drawing.point(16, 16); thi

13、s.label2.name = "label2" this.label2.size = new system.drawing.size(64, 24); this.label2.tabindex = 0; this.label2.text = "偵聽端口:" this.label2.textalign = system.drawing.contentalignment.middleleft; / / btnstart / this.btnstart.location = new system.drawing.point(128, 16); this.bt

14、nstart.name = "btnstart" this.btnstart.size = new system.drawing.size(48, 21); this.btnstart.tabindex = 2; this.btnstart.text = "開始" this.btnstart.click += new system.eventhandler(this.btnstart_click); / / lblstatus / this.lblstatus.location = new system.drawing.point(48, 48); th

15、is.lblstatus.name = "lblstatus" this.lblstatus.size = new system.drawing.size(264, 32); this.lblstatus.tabindex = 5; this.lblstatus.text = "就緒" this.lblstatus.click += new system.eventhandler(this.lblstatus_click); / / label4 / this.label4.location = new system.drawing.point(16,

16、48); this.label4.name = "label4" this.label4.size = new system.drawing.size(40, 16); this.label4.tabindex = 4; this.label4.text = "狀態(tài):" / / label1 / this.label1.location = new system.drawing.point(16, 88); this.label1.name = "label1" this.label1.size = new system.drawin

17、g.size(296, 32); this.label1.tabindex = 6; this.label1.text = "注意:彈出接收文件另存為對話框后請盡快確定保存位置,以免連接因等待太久而超時。" / / btnstop / this.btnstop.enabled = false; this.btnstop.location = new system.drawing.point(176, 16); this.btnstop.name = "btnstop" this.btnstop.size = new system.drawing.size

18、(48, 21); this.btnstop.tabindex = 3; this.btnstop.text = "停止" this.btnstop.click += new system.eventhandler(this.btnstop_click); / / dlgsavefile / this.dlgsavefile.filter = "所有文件|*.*" this.dlgsavefile.title = "接收文件另存為" / / receiver / this.autoscalebasesize = new system.

19、drawing.size(6, 14); this.clientsize = new system.drawing.size(320, 133); this.controls.add(this.label1); this.controls.add(this.lblstatus); this.controls.add(this.label4); this.controls.add(this.btnstart); this.controls.add(this.txtport); this.controls.add(this.label2); this.controls.add(this.btnst

20、op); this.formborderstyle = system.windows.forms.formborderstyle.fixedsingle; this.maximizebox = false; this.minimizebox = false; this.name = "receiver" this.text = "接收方" this.load += new system.eventhandler(this.receiver_load); this.resumelayout(false); this.performlayout();#end

21、region#region 程序邏輯private thread worker;private int port;private void start()try / 捕獲線程中止異常setstatus("開始偵聽端口");tcplistener tcplistener = new tcplistener(ipaddress.any, port);try / 捕獲開始偵聽時出現(xiàn)的錯誤,例如端口已被占用tcplistener.start();catch (exception e)setstatus("偵聽端口出錯:" + e.message);invokec

22、ancel();return;try / 確保停止偵聽setstatus("等待傳入連接中");while (true) / 一直循環(huán),直到線程被中止。但每次只處理一個連接/ 沒有等待傳入的連接就一直等待。不使用異步模型以提高程序可讀性while (!tcplistener.pending()thread.sleep(100);tcpclient tcpclient = tcplistener.accepttcpclient();tcpclient.nodelay = true;tcpclient.receivetimeout = 30000;binaryreader re

23、ader = new binaryreader(tcpclient.getstream();try / 捕獲文件傳輸中途出現(xiàn)的錯誤,并確保關(guān)閉連接string filename = reader.readstring();long total = reader.readint64();setstatus("收到文件傳輸請求,文件名" + filename +",大小" + total.tostring() + " 字節(jié)");string saveas = getsavefile(filename);if (saveas = null)

24、setstatus("操作已取消。等待下一連接中");/ finally 段會幫忙關(guān)閉連接elsesetstatus("接收文件中");filestream fs = file.create(saveas);trybyte buffer = new byte8192;int len;while (total > 0)len = reader.read(buffer, 0, 8192);if (len = 0)throw new ioexception("發(fā)送方中止了連接");fs.write(buffer, 0, len);se

25、tstatus("接收文件中- 剩余" +(total -= len).tostring() + " 字節(jié)");setstatus("文件已保存至" + saveas +"。等待下一連接中");finallyfs.close();catch (exception e)setstatus("文件傳輸中途出錯:" + e.message);invokecancel();finallytry reader.close(); catch try tcpclient.close(); catch fina

26、llytcplistener.stop();catch (threadabortexception)setstatus("用戶中斷");invokecancel();finallyworker = null;#endregion/ <summary>/ 應(yīng)用程序的主入口點。/ </summary>stathreadstatic void main() application.run(new receiver();private void btnstart_click(object sender, system.eventargs e)port = 0

27、;tryport = int.parse(txtport.text);if (port < 1 | port > 65535) throw new argumentoutofrangeexception();catchmessagebox.show("請輸入有效的端口號。");txtport.focus();txtport.selectall();return;btnstart.enabled = false;btnstop.enabled = true;worker = new thread(new threadstart(start);worker.isbackground = true;worker.start();private void btnstop_click(object sender, system.eventargs e)try worker.abort(); catch protected override void onclosing(canceleventargs e)if (btnstop.enabled)e.cancel = true;base.onclosing(e);#endregion#region 線程安全ui操作代碼private delegate voi

溫馨提示

  • 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論