計算機(jī)網(wǎng)絡(luò)課程設(shè)計_報告.doc_第1頁
計算機(jī)網(wǎng)絡(luò)課程設(shè)計_報告.doc_第2頁
計算機(jī)網(wǎng)絡(luò)課程設(shè)計_報告.doc_第3頁
計算機(jī)網(wǎng)絡(luò)課程設(shè)計_報告.doc_第4頁
計算機(jī)網(wǎng)絡(luò)課程設(shè)計_報告.doc_第5頁
已閱讀5頁,還剩32頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

實(shí) 驗 報 告實(shí)驗名稱: 計算機(jī)網(wǎng)絡(luò)課程設(shè)計 學(xué)生姓名: xxxxxxxxxxxxxxx 專 業(yè): xxxxxxxxxxxxxxx 班 級: xxxxxxxxxxxxxxx 學(xué) 號: xxxxxxxxxxxxxxx 指導(dǎo)教師: xxxxxxxxxxxxxxx 實(shí)驗成績: 實(shí)驗地點(diǎn): 實(shí)驗時間: 2016 年 5 月 6 日一、實(shí)驗?zāi)康呐c實(shí)驗要求1、實(shí)驗?zāi)康膶旧铣橄蟮母拍钆c具體實(shí)現(xiàn)技術(shù)結(jié)合,通過網(wǎng)絡(luò)軟件編程的實(shí)踐,深入理解理論課上學(xué)習(xí)到的ARP、IP、TCP等重要網(wǎng)絡(luò)協(xié)議的原理,通過自己動手編程封裝與發(fā)送這些數(shù)據(jù)包,加深對網(wǎng)絡(luò)協(xié)議的理解,掌握協(xié)議幀的結(jié)構(gòu)和工作原理及其對協(xié)議棧的貢獻(xiàn)。2、實(shí)驗要求網(wǎng)絡(luò)課程設(shè)計包含兩個部分的內(nèi)容:題目一是數(shù)據(jù)包的封裝發(fā)送和解析(ARP/IP/TCP),要求使用Winpcap技術(shù)和Socket技術(shù),根據(jù)ARP/IP/TCP幀的結(jié)構(gòu),封裝數(shù)據(jù)包發(fā)送到局域網(wǎng)中。另外要捕獲網(wǎng)絡(luò)中的TCP/IP/ARP數(shù)據(jù)包,解析數(shù)據(jù)包的內(nèi)容,并將結(jié)果顯示,并同時寫入日志文件。題目二是從可選題目中選擇一個,可選題目均是網(wǎng)絡(luò)應(yīng)用小程序,要求小組使用網(wǎng)絡(luò)編程技術(shù)設(shè)計并實(shí)現(xiàn)一個網(wǎng)絡(luò)應(yīng)用程序,加深對網(wǎng)絡(luò)協(xié)議協(xié)的理解,并鍛煉網(wǎng)絡(luò)編程能力。二、實(shí)驗設(shè)備(環(huán)境)及要求1、實(shí)驗硬件設(shè)備:計算機(jī)型號:聯(lián)想ThinkPad T430u處理器型號:Intel i5 主頻:1.8Hz網(wǎng)卡型號:(1)Realtek PCIe GBE (2)Broadcom 802.11n2、實(shí)驗軟件要求:操作系統(tǒng):Windows10應(yīng)用軟件:Visual Studio 2015 Pro3、小組成員及分工:三、實(shí)驗內(nèi)容與步驟1、實(shí)驗1:數(shù)據(jù)包的封裝發(fā)送和解析(ARP/IP/TCP)(1)實(shí)驗內(nèi)容1) 程序目標(biāo):根據(jù)IP幀的結(jié)構(gòu),封裝IP數(shù)據(jù)包發(fā)送到局域網(wǎng)中。并捕獲網(wǎng)絡(luò)中的IP數(shù)據(jù)包,解析數(shù)據(jù)包的內(nèi)容,并將結(jié)果顯示,并同時寫入日志文件。2) 程序功能:以命令行形式運(yùn)行在標(biāo)準(zhǔn)輸出中顯示捕獲的IP報文的首部字段的內(nèi)容。使用winpcap訪問網(wǎng)卡,手動封裝定義IP首部的數(shù)據(jù)結(jié)構(gòu)填充數(shù)據(jù)包,發(fā)送數(shù)據(jù)包,捕獲數(shù)據(jù)包使用winpcap,捕獲IP數(shù)據(jù)包(2)主要步驟1) 總體設(shè)計:a. 獲取設(shè)備列表并打印,打開所選擇的適配器;b. 準(zhǔn)備工作:定義ip相關(guān)的結(jié)構(gòu)體、打開要存放結(jié)果的文件,設(shè)置過濾器,手寫ip數(shù)據(jù)報(內(nèi)容有無效的MAC源和目的地址,和均為本機(jī)地址的ip源地址和目的地址,即發(fā)給自己一個ip報文),設(shè)置抓到數(shù)據(jù)報的解析和輸出到文件的操作函數(shù)(解析ip報,打印并寫入文件:報文的版本、協(xié)議、源和目的地址等)。c. 發(fā)包、抓包。d. 分析獲取的數(shù)據(jù)。2) 具體實(shí)現(xiàn):#define HAVE_REMOTE#include pcap.h#include remote-ext.h#include stdio.h#include stdlib.h/*4字節(jié)的IP地址*/typedef struct ip_addressu_char byte1;u_char byte2;u_char byte3;u_char byte4;ip_address;/*IPv4首部*/typedef struct ip_headeru_char ver_ihl; /版本(4bits)+首部長度(4bits)u_char tos; /服務(wù)類型u_short tlen; /總長類型u_short identification; /標(biāo)識u_short flags_fo; /標(biāo)志位+段偏移量u_char ttl; /存活時間u_char proto; /協(xié)議u_short crc; /首部校驗和ip_address daddr; /目的地址ip_address saddr; /源地址u_int op_pad; /選項與填充ip_header;void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);/* packet handler 函數(shù)原型 */void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);int main()pcap_if_t *alldevs;pcap_if_t *d;int inum;int i=0;pcap_t *adhandle;char errbufPCAP_ERRBUF_SIZE;u_int netmask;char packet_filter = ip; /抓包類型struct bpf_program fcode;pcap_dumper_t *dumpfile;/* 獲取本機(jī)設(shè)備列表 */if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) = -1)fprintf(stderr,Error in pcap_findalldevs: %sn, errbuf);exit(1);/* 打印適配器列表 */for(d=alldevs; d; d=d-next)printf(%d. %s, +i, d-name);if (d-description)printf( (%s)n, d-description);elseprintf( (No description available)n);if(i=0)printf(nNo interfaces found! Make sure WinPcap is installed.n);return -1;printf(Enter the interface number (1-%d):,i);scanf(%d, &inum);if(inum i)printf(nInterface number out of range.n);/* 釋放設(shè)備列表 */pcap_freealldevs(alldevs);return -1;/* 跳轉(zhuǎn)到選中的適配器 */for(d=alldevs, i=0; inext, i+);/* 打開設(shè)備 */if ( (adhandle= pcap_open(d-name, / 設(shè)備名 65536, / 65535保證能捕獲到不同數(shù)據(jù)鏈路層上每個數(shù)據(jù)包的全部內(nèi)容PCAP_OPENFLAG_PROMISCUOUS, / 混雜模式1000, / 讀取超時時間NULL, / 遠(yuǎn)程機(jī)器驗證errbuf / 錯誤緩沖池 ) ) = NULL)fprintf(stderr,nUnable to open the adapter. %s is not supported by WinPcapn, d-name);/* 釋放設(shè)備列表 */pcap_freealldevs(alldevs);return -1;/* 打開堆文件*/dumpfile = pcap_dump_open(adhandle, D:save.txt);if(dumpfile=NULL)fprintf(stderr,nError opening output filen);return -1;/* 檢查數(shù)據(jù)鏈路層,只考慮以太網(wǎng) */if(pcap_datalink(adhandle)!=DLT_EN10MB)fprintf(stderr, nThis program works only on Ethernet networds.n);/* 釋放設(shè)備列表 */pcap_freealldevs(alldevs);return -1;if(d-addresses != NULL)/獲得接口第一個地址的掩碼netmask = (struct sockaddr_in *)(d-addresses-netmask)-sin_addr.S_un.S_addr;else/如果接口沒有地址,那么我們假設(shè)一個C類的掩碼netmask=0xffffff; /編譯過濾器if(pcap_compile(adhandle,&fcode,packet_filter,1,netmask)0)fprintf(stderr,nUnable to compile the packet filter. Check the syntax.n);/釋放設(shè)備列表pcap_freealldevs(alldevs);return -1;/設(shè)置過濾器if(pcap_setfilter(adhandle,&fcode)description);/釋放設(shè)備列表pcap_freealldevs(alldevs);/*手寫數(shù)據(jù)包*/u_char packet100;/*假設(shè)在以太網(wǎng)上,設(shè)置MAC的目的地址為1:1:1:1:1:1*/packet0=1;packet1=1;packet2=1;packet3=1;packet4=1;packet5=1;/*設(shè)置MAC的源地址為2:2:2:2:2:2*/packet6=2;packet7=2;packet8=2;packet9=2;packet10=2;packet11=2;/*設(shè)置ip類型*/packet12=0x08;packet13=0x00;packet14=0x45;packet15=0x20;packet16=0x00;packet17=0x28;packet18=0xcb;packet19=0x16;packet20=0x00;packet21=0x00;packet22=0x2e;packet23=0x06;packet24=0x3e;packet25=0xe6;packet26=0xc0;/192packet27=0xa8;/168packet28=0x01;/1packet29=0x64;/100packet30=0xc0;packet31=0xa8;packet32=0x01;packet33=0x64;packet34=0x8f;packet35=0x50; /*填充剩下的內(nèi)容*/for(i=36;its.tv_sec;ltime=localtime(&local_tv_sec);strftime( timestr, sizeof timestr, %H:%M:%S, ltime);/打印數(shù)據(jù)包的時間戳和長度printf(%s,%.6d len:%dn, timestr, header-ts.tv_usec, header-len);/獲得IP數(shù)據(jù)包頭部的位置ih = (ip_header*)(pkt_data+14);/以太網(wǎng)頭部長度/* 打印 IP 地址和 UDP 端口 */printf(版本+首部長度:%u,ih-ver_ihl);printf(協(xié)議:%u,ih-proto);printf(首部校驗和:%u,ih-crc);printf(目的地址:%u.%u.%u.%u, ih-daddr.byte1,ih-daddr.byte2,ih-daddr.byte3,ih-daddr.byte4); printf(源地址:%u.%u.%u.%un, ih-saddr.byte1,ih-saddr.byte2,ih-saddr.byte3,ih-saddr.byte4);FILE *fp;fp=fopen(D:jiexi.txt, a+);fprintf(fp,解析結(jié)果:);fprintf(fp,版本+首部長度:%u,ih-ver_ihl);fprintf(fp,協(xié)議:%u,ih-proto);fprintf(fp,首部校驗和:%u,ih-crc);fprintf(fp,目的地址:%u.%u.%u.%u, ih-daddr.byte1,ih-daddr.byte2,ih-daddr.byte3,ih-daddr.byte4); fprintf(fp,源地址:%u.%u.%u.%un, ih-saddr.byte1,ih-saddr.byte2,ih-saddr.byte3,ih-saddr.byte4);fclose(fp);2、實(shí)驗2:子網(wǎng)內(nèi)文件傳送(1)實(shí)驗內(nèi)容(明確的實(shí)驗內(nèi)容)設(shè)計并實(shí)現(xiàn)一個局域網(wǎng)內(nèi)部的文件傳送工具,使用TCP協(xié)議進(jìn)行可靠文件傳輸。以圖形界面運(yùn)行,不同結(jié)點(diǎn)上文件自動同步(2)主要步驟(詳細(xì)的實(shí)驗步驟(系統(tǒng)/方法/算法等),圖文結(jié)合)1) 問題定義:實(shí)現(xiàn)一個局域網(wǎng)內(nèi)的文件傳送與聊天的軟件2) 需求分析:局域網(wǎng)內(nèi)的文件傳送應(yīng)當(dāng)具備以下功能:l 在線用戶的及時發(fā)現(xiàn)和更新l 選擇一對一或者一對多對話模式l 與選擇用戶進(jìn)行聊天和文件傳輸l 用戶下線通知與更新用戶列表3) 系統(tǒng)設(shè)計:參考飛鴿傳書的實(shí)現(xiàn)原理,總結(jié)設(shè)計方案如下:l 架構(gòu)設(shè)計:本系統(tǒng)采用本地應(yīng)用程序設(shè)計,僅適用于在同一局域網(wǎng)的主機(jī)通信;l 互相發(fā)現(xiàn):本系統(tǒng)運(yùn)行時啟動局域網(wǎng)廣播線程,發(fā)出包含主機(jī)名稱、主機(jī)IP以及新加入標(biāo)識符的廣播表明自己新加入;l 用戶列表:監(jiān)聽到其他用戶的廣播信息后更新列表,顯示當(dāng)前在線的用戶名和用戶IP;l 更新列表:在系統(tǒng)退出時會廣播本機(jī)信息,標(biāo)識表明自己離開,其余在線用戶接收到信息后更新提示該用戶下線;l 選擇用戶:可以單獨(dú)選擇或者多項選擇用戶列表中的用戶;l 發(fā)送消息:選擇用戶后,在輸入框中輸入聊天信息,點(diǎn)擊“發(fā)送”按鈕系統(tǒng)會發(fā)送UDP數(shù)據(jù)包給對應(yīng)用戶的ip地址,端口是8011;l 接受消息:系統(tǒng)實(shí)時監(jiān)聽8011端口以接受不同用戶發(fā)來的消息并展示在聊天窗口中;l 發(fā)送文件:選擇指定用戶,點(diǎn)擊“發(fā)送文件”按鈕,選擇本機(jī)相應(yīng)文件后,與目標(biāo)用戶建立TCP連接,發(fā)送字節(jié)流。端口為8011;l 接受文件:監(jiān)聽到有TCP連接請求時建立實(shí)時連接,系統(tǒng)提示用戶是否接收文件,選擇接收后選擇相應(yīng)路徑與文件名后進(jìn)行接收,接收成功后再聊天窗口顯示提示信息。4) 詳細(xì)設(shè)計:1 局域網(wǎng)用戶列表的建立:軟件啟動后定時的使用UDP協(xié)議向55這個廣播地址發(fā)送廣播包,端口設(shè)置為8001。廣播包內(nèi)容包含主機(jī)名、IP、標(biāo)識符等信息,已啟動軟件的用戶通過8001端口收到此廣播包后,就會在自己的用戶列表中添加這個用戶的用戶名、IP地址等信息,從而局域網(wǎng)內(nèi)的本系統(tǒng)用戶都能建立起用戶列表;2 傳送與接收信息:傳送聊天信息時同樣使用UDP協(xié)議,用戶填寫消息后點(diǎn)擊按鈕調(diào)用發(fā)送消息的函數(shù),然后通過8011端口發(fā)送UDP包。系統(tǒng)在啟動時通過開辟新線程來監(jiān)聽8011端口,時刻監(jiān)聽發(fā)送到本機(jī)的信息。3 發(fā)送文件:用戶發(fā)送文件時建立一個帶參數(shù)的線程來發(fā)送文件,調(diào)用相應(yīng)的TCP發(fā)送函數(shù)來發(fā)送文件,使用的是8001端口,由于協(xié)議不同因此與之前的廣播并不沖突;4 接收文件:軟件啟動時開啟一個線程用來接收文件,此線程在正常工作條件下處于阻塞監(jiān)聽狀態(tài),直到收到建立TCP連接的請求,之后提示用戶收到文件選擇是否接受,之后選擇保存路徑,進(jìn)行接收。5 用戶離開:用戶離線時發(fā)送一個離線廣播包到55的8001端口,包中含有LEAVE的標(biāo)識符,收到此廣播包的用戶,根據(jù)包中的IP地址刪除用戶列表中相應(yīng)的用戶信息,并在聊天窗口顯示“XXX用戶已經(jīng)離開”;6 聊天記錄保存:考慮到用戶有保存聊天記錄的需求,在聊天記錄右側(cè)點(diǎn)擊“保存”按鈕,可以將當(dāng)前聊天記錄保存為txt文件到用戶指定目錄中?;螯c(diǎn)擊“清空”,清空當(dāng)前用戶聊天記錄。5) 具體實(shí)現(xiàn):(C#)using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace IPMessage public partial class frmMain : Form /用戶實(shí)體 public class User private string localname; /用戶名 public string localName get return localname; set localname = value; private string localip; /用戶IP public string localIP get return localip; set localip = value; /用戶數(shù)組聲明 public static List Userslist = new List(); /獲得本機(jī)名和本機(jī)IP public User getInfo() User u = new User(); string localName = Dns.GetHostName();/獲取主機(jī)名 /Console.WriteLine(主機(jī)名:0, localName); u.localName = localName; IPHostEntry localHost = Dns.GetHostEntry(localName); /輸出對應(yīng)的IP地址 IPAddress localIP = null; for (int i = 0; i 1) bool exist = false; if (Userslist!=null) foreach (User us in Userslist) if (us.localIP = sArray2) exist = true; break; if (exist = false & sArray0=NEW) User u = new User(); u.localName = sArray1; u.localIP = sArray2; if (u != null) Userslist.Add(u); this.rbChatContent.AppendText(新上線用戶:+u.localName + n); this.lbUser.Items.Add(u.localName + | + u.localIP); Thread.Sleep(100); else if(sArray0 = LEAVE) User u = new User(); u.localName = sArray1; u.localIP = sArray2; if (u != null) Userslist.Remove(u); this.rbChatContent.AppendText(用戶: + u.localName + 離開+n); this.lbUser.Items.Remove(u.localName + | + u.localIP); Thread.Sleep(200); / / 通過TcpClient滿足多用戶發(fā)送文件 / / public void tcpSend(List ips) FileStream fs = new FileStream(filePath, FileMode.Open); foreach(string ip in ips) TcpClient client = new TcpClient(); client.Connect(ip, 8001); NetworkStream ns = client.GetStream(); int size = 0; long len = 0; while (len fs.Length) byte buffer = new byte1024; size = fs.Read(buffer, 0, buffer.Length); ns.Write(buffer, 0, size); len += size; fs.Flush(); ns.Flush(); fs.Close(); ns.Close(); / / 使用TcpClient接收文件 / public void RecevieFile() TcpListener listener = new TcpListener(IPAddress.Any, 8001); listener.Start(); while(true) try int size = 0; int len = 0; TcpClient client = listener.AcceptTcpClient(); if (client.Connected) this.rbChatContent.AppendText(連接成功); NetworkStream stream = client.GetStream(); if (stream != null) DialogResult rs; rs = MessageBox.Show(你收到一個文件,是否接收?,提示,MessageBoxButtons.YesNo); if (rs = DialogResult.Yes) SaveFileDialog sfd = new SaveFileDialog(); if (sfd.ShowDialog(this) = System.Windows.Forms.DialogResult.OK) string fileSavePath = sfd.FileName;/獲得用戶保存文件的路徑 FileStream fs = new FileStream(fileSavePath, FileMode.Create, FileAccess.Write); byte buffer = new byte1024; while (size = stream.Read(buffer, 0, buffer.Length) 0) fs.Write(buffer, 0, size); len += size; fs.Flush(); stream.Flush(); stream.Close(); client.Close(); this.rbChatContent.AppendText(文件接受成功 + fileSavePath + n); catch(Exception ex) MessageBox.Show(ex.ToString(); / / 主函數(shù) / public frmMain() InitializeComponent(); Thread seThread = new Thread(this.broadMessage); seThread.Start(); Thread reThread = new Thread(this.reciveBroad); reThread.Start(); Thread rfThread = new Thread(this.RecevieFile); rfThread.Start(); Thread rmThread = new Thread(this.listenMsg); rmThread.Start(); /離開發(fā)送廣播 private void frmMain_FormClosing(object sender, FormClosingEventArgs e) isBroad = false; this.br

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論