


版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、WORD格式實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)名稱(chēng) :計(jì)算機(jī)網(wǎng)絡(luò)課程設(shè)計(jì)學(xué)生* : *x專(zhuān)業(yè): *x班級(jí): *x學(xué)號(hào): *x指導(dǎo)教師 :*x實(shí)驗(yàn)成績(jī) :實(shí)驗(yàn)地點(diǎn) :實(shí)驗(yàn)時(shí)間 :2021年 5月6 日專(zhuān)業(yè)資料整理WORD格式一、實(shí)驗(yàn)?zāi)康呐c實(shí)驗(yàn)要求1、實(shí)驗(yàn)?zāi)康膶?shū)本上抽象的概念與具體實(shí)現(xiàn)技術(shù)結(jié)合,通過(guò)網(wǎng)絡(luò)軟件編程的實(shí)踐,深入理解理論課上學(xué)習(xí)到的ARP、IP、TCP 等重要網(wǎng)絡(luò)協(xié)議的原理,通過(guò)自己動(dòng)手編程封裝與發(fā)送這些數(shù)據(jù)包, 加深對(duì)網(wǎng)絡(luò)協(xié)議的理解, 掌握協(xié)議幀的構(gòu)造和工作原理及其對(duì)協(xié)議棧的奉獻(xiàn)。2、實(shí)驗(yàn)要求網(wǎng)絡(luò)課程設(shè)計(jì)包含兩個(gè)局部的內(nèi)容:題目一是數(shù)據(jù)包的封裝發(fā)送和解析ARP/IP/TCP,要求使用 Winpcap 技術(shù)
2、和 Socket 技術(shù),根據(jù) ARP/IP/TCP 幀的構(gòu)造,封裝數(shù)據(jù)包發(fā)送到局域網(wǎng)中。另外要捕獲網(wǎng)絡(luò)中的 TCP/IP/ARP 數(shù)據(jù)包,解析數(shù)據(jù)包的內(nèi)容,并將結(jié)果顯示,并同時(shí)寫(xiě)入日志文件。題目二是從可選題目中選擇一個(gè),可選題目均是網(wǎng)絡(luò)應(yīng)用小程序,要求小組使用網(wǎng)絡(luò)編程技術(shù)設(shè)計(jì)并實(shí)現(xiàn)一個(gè)網(wǎng)絡(luò)應(yīng)用程序, 加深對(duì)網(wǎng)絡(luò)協(xié)議協(xié)的理解, 并鍛煉網(wǎng)絡(luò)編程能力。二、實(shí)驗(yàn)設(shè)備環(huán)境及要求1、實(shí)驗(yàn)硬件設(shè)備:計(jì)算機(jī)型號(hào):聯(lián)想ThinkPad T430u處理器型號(hào): Intel i5 主頻: 1.8Hz網(wǎng)卡型號(hào): (1)Realtek PCIe GBE (2)Broadcom 802.11n2、實(shí)驗(yàn)軟件要求:專(zhuān)業(yè)資料整
3、理WORD格式操作系統(tǒng): Windows10專(zhuān)業(yè)資料整理WORD格式應(yīng)用軟件: Visual Studio 2021 Pro3、小組成員及分工:三、實(shí)驗(yàn)內(nèi)容與步驟1、實(shí)驗(yàn) 1:數(shù)據(jù)包的封裝發(fā)送和解析ARP/IP/TCP1實(shí)驗(yàn)內(nèi)容1) 程序目標(biāo):根據(jù) IP 幀的構(gòu)造,封裝 IP 數(shù)據(jù)包發(fā)送到局域網(wǎng)中。 并捕獲網(wǎng)絡(luò)中的IP 數(shù)據(jù)包,解析數(shù)據(jù)包的內(nèi)容,并將結(jié)果顯示,并同時(shí)寫(xiě)入日志文件。2) 程序功能:以命令行形式運(yùn)行在標(biāo)準(zhǔn)輸出中顯示捕獲的 IP 報(bào)文的首部字段的內(nèi)容。使用 winpcap 訪(fǎng)問(wèn)網(wǎng)卡,手動(dòng)封裝定義 IP 首部的數(shù)據(jù)構(gòu)造填充數(shù)據(jù)包,發(fā)送數(shù)據(jù)包,捕獲數(shù)據(jù)包使用 winpcap,捕獲 IP
4、數(shù)據(jù)包2主要步驟1) 總體設(shè)計(jì):a. 獲取設(shè)備列表并打印,翻開(kāi)所選擇的適配器;b. 準(zhǔn)備工作:定義 ip 相關(guān)的構(gòu)造體、翻開(kāi)要存放結(jié)果的文件,設(shè)置過(guò)濾器,手寫(xiě) ip 數(shù)據(jù)報(bào)內(nèi)容有無(wú)效的 MAC 源和目的地址, 和均為本機(jī)地址的 ip 源地址和目的地址,即發(fā)給自己一個(gè) ip 報(bào)文,設(shè)置抓到數(shù)據(jù)報(bào)的解析和輸出到文件的操作函數(shù)解析 ip 報(bào),打印并寫(xiě)入文件:報(bào)文的版本、協(xié)議、源和目專(zhuān)業(yè)資料整理WORD格式的地址等。c. 發(fā)包、抓包。d. 分析獲取的數(shù)據(jù)。獲取設(shè)備列表翻開(kāi)選擇適配器過(guò)濾數(shù)據(jù)包解析數(shù)據(jù)包2) 具體實(shí)現(xiàn):#define HAVE_REMOTE#include "pcap.h&qu
5、ot;#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 首部 */專(zhuān)業(yè)資料整理WORD格式typedef struct ip_header專(zhuān)業(yè)資料整理WORD格式u_char ver_ihl;/版本4bits +首部長(zhǎng)度4bits專(zhuān)業(yè)資料整理WOR
6、D格式u_char tos;/效勞類(lèi)型專(zhuān)業(yè)資料整理WORD格式u_short tlen;/ 總長(zhǎng)類(lèi)型專(zhuān)業(yè)資料整理WORD格式u_short identification;/ 標(biāo)識(shí)專(zhuān)業(yè)資料整理WORD格式u_short flags_fo;/標(biāo)志位+段偏移量專(zhuān)業(yè)資料整理WORD格式u_char ttl;/ 存活時(shí)間u_char proto;/協(xié)議u_short crc;/首部校驗(yàn)和ip_address daddr;/目的地址ip_address saddr;/源地址u_int op_pad;/ 選項(xiàng)與填充ip_header;void packet_handler(u_char *param, co
7、nst 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;專(zhuān)業(yè)資料整理WORD
8、格式char packet_filter = "ip"/ 抓包類(lèi)型專(zhuān)業(yè)資料整理WORD格式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("
9、%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)
10、;scanf("%d", &inum);if(inum < 1 | inum > i)專(zhuān)業(yè)資料整理WORD格式printf("nInterface number out of range.n");/* 釋放設(shè)備列表*/pcap_freealldevs(alldevs);return -1;/* 跳轉(zhuǎn)到選中的適配器*/for(d=alldevs, i=0; i< inum-1 ;d=d->next, i+);/* 翻開(kāi)設(shè)備*/if ( (adhandle= pcap_open(d->name,/ 設(shè)備名65536,/
11、65535 保證能捕獲到不同數(shù)據(jù)鏈路層上每個(gè)數(shù)據(jù)包的全部?jī)?nèi)容專(zhuān)業(yè)資料整理WORD格式PCAP_OPENFLAG_PROMISCUOUS,/ 混雜模式專(zhuān)業(yè)資料整理WORD格式1000,/ 讀取超時(shí)時(shí)間專(zhuān)業(yè)資料整理WORD格式NULL,/ 遠(yuǎn)程機(jī)器驗(yàn)證專(zhuān)業(yè)資料整理WORD格式errbuf) ) = NULL)/ 錯(cuò)誤緩沖池專(zhuān)業(yè)資料整理WORD格式fprintf(stderr,"nUnableto open the adapter. %s is not supported by WinPcapn",d->name);/* 釋放設(shè)備列表*/pcap_freealldevs(a
12、lldevs);return -1;/* 翻開(kāi)堆文件 */dumpfile = pcap_dump_open(adhandle, "D:save.txt");if(dumpfile=NULL)專(zhuān)業(yè)資料整理WORD格式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
13、 networds.n");/* 釋放設(shè)備列表*/pcap_freealldevs(alldevs);return -1;if(d->addresses != NULL)/獲得接口第一個(gè)地址的掩碼netmask = (struct sockaddr_in *)(d->addresses->netmask)->sin_addr.S_un.S_addr;else/如果接口沒(méi)有地址,那么我們假設(shè)一個(gè)C 類(lèi)的掩碼netmask=0xffffff;/編譯過(guò)濾器if(pcap_compile(adhandle,&fcode,packet_filter,1,netm
14、ask)<0)fprintf(stderr,"nUnable to compile the packet filter. Check the syntax.n");/釋放設(shè)備列表pcap_freealldevs(alldevs);return -1;/設(shè)置過(guò)濾器if(pcap_setfilter(adhandle,&fcode)<0)fprintf(stderr,"nError setting the filter.n");專(zhuān)業(yè)資料整理WORD格式/釋放設(shè)備列表pcap_freealldevs(alldevs);return -1;p
15、rintf("nlistening on %s.n", d->description);/釋放設(shè)備列表pcap_freealldevs(alldevs);/* 手寫(xiě)數(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;packet1
16、1=2;/* 設(shè)置 ip 類(lèi)型 */packet12=0x08;packet13=0x00;packet14=0x45;專(zhuān)業(yè)資料整理WORD格式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;/100pack
17、et30=0xc0;packet31=0xa8;packet32=0x01;packet33=0x64;packet34=0x8f;packet35=0x50;/* 填充剩下的內(nèi)容*/for(i=36;i<100;i+)packeti=i%256;/* 發(fā)送數(shù)據(jù)包 */if (pcap_sendpacket(adhandle, packet, 100 /* size */) != 0)專(zhuān)業(yè)資料整理WORD格式fprintf(stderr,"nError sending the packet: n", pcap_geterr(adhandle);return 0;els
18、eprintf("Send successed");/* 開(kāi)場(chǎng)捕獲*/pcap_loop(adhandle, 0, packet_handler,(unsigned char *)dumpfile);/回調(diào)方式捕獲數(shù)據(jù)包pcap_close(adhandle);return 0;/*每次捕獲到數(shù)據(jù)包時(shí),libpcap 都會(huì)自動(dòng)調(diào)用這個(gè)回調(diào)函數(shù)*/void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)struct tm *ltime;char ti
19、mestr16;ip_header *ih;u_int ip_len;time_t local_tv_sec;/保存數(shù)據(jù)包到文件中pcap_dump(u_char*)param, header, pkt_data);/* 將時(shí)間戳轉(zhuǎn)換成可識(shí)別的格式*/local_tv_sec = header->ts.tv_sec;ltime=localtime(&local_tv_sec);專(zhuān)業(yè)資料整理WORD格式strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);/打印數(shù)據(jù)包的時(shí)間戳和長(zhǎng)度printf("%
20、s,%.6d len:%dn", timestr, header->ts.tv_usec, header->len);/獲得 IP 數(shù)據(jù)*部的位置ih = (ip_header*)(pkt_data+14);/ 以太網(wǎng)頭部長(zhǎng)度/* 打印IP 地址和UDP 端口*/printf(" 版本 +首部長(zhǎng)度 :%u",ih->ver_ihl);printf(" 協(xié)議 :%u",ih->proto);專(zhuān)業(yè)資料整理WORD格式printf("首部校驗(yàn)和:%u",ih->crc);專(zhuān)業(yè)資料整理WORD格式pri
21、ntf("目的地址:%u.%u.%u.%u",專(zhuān)業(yè)資料整理WORD格式ih->daddr.byte1,ih->daddr.byte2,ih->daddr.byte3,ih->daddr.byte4);專(zhuān)業(yè)資料整理WORD格式printf(" 源 地 址 ih->saddr.byte1,ih->saddr.byte2,ih->saddr.byte3,ih->saddr.byte4);:%u.%u.%u.%un",專(zhuān)業(yè)資料整理WORD格式FILE *fp;fp=fopen("D:jiexi.txt&q
22、uot;, "a+");fprintf(fp," 解析結(jié)果: ");fprintf(fp," 版本 +首部長(zhǎng)度 :%u",ih->ver_ihl);fprintf(fp," 協(xié)議 :%u",ih->proto);fprintf(fp," 首部校驗(yàn)和 :%u",ih->crc);fprintf(fp,"目的地址:%u.%u.%u.%u",ih->daddr.byte1,ih->daddr.byte2,ih->daddr.byte3,ih-&g
23、t;daddr.byte4);fprintf(fp,"源地址:%u.%u.%u.%un",ih->saddr.byte1,ih->saddr.byte2,ih->saddr.byte3,ih->saddr.byte4);fclose(fp);專(zhuān)業(yè)資料整理WORD格式2、實(shí)驗(yàn) 2:子網(wǎng)內(nèi)文件傳送1實(shí)驗(yàn)內(nèi)容明確的實(shí)驗(yàn)內(nèi)容設(shè)計(jì)并實(shí)現(xiàn)一個(gè)局域網(wǎng)內(nèi)部的文件傳送工具,使用 TCP 協(xié)議進(jìn)展可靠文件傳輸。以圖形界面運(yùn)行,不同結(jié)點(diǎn)上文件自動(dòng)同步2主要步驟詳細(xì)的實(shí)驗(yàn)步驟系統(tǒng)/方法 /算法等,圖文結(jié)合1) 問(wèn)題定義:實(shí)現(xiàn)一個(gè)局域網(wǎng)內(nèi)的文件傳送與聊天的軟件2) 需求分析:
24、局域網(wǎng)內(nèi)的文件傳送應(yīng)當(dāng)具備以下功能:在線(xiàn)用戶(hù)的及時(shí)發(fā)現(xiàn)和更新選擇一對(duì)一或者一對(duì)多對(duì)話(huà)模式與選擇用戶(hù)進(jìn)展聊天和文件傳輸用戶(hù)下線(xiàn)通知與更新用戶(hù)列表3) 系統(tǒng)設(shè)計(jì):參考飛鴿傳書(shū)的實(shí)現(xiàn)原理,總結(jié)設(shè)計(jì)方案如下:架構(gòu)設(shè)計(jì):本系統(tǒng)采用本地應(yīng)用程序設(shè)計(jì),僅適用于在同一局域網(wǎng)的主機(jī)通信;互相發(fā)現(xiàn):本系統(tǒng)運(yùn)行時(shí)啟動(dòng)局域網(wǎng)播送線(xiàn)程,發(fā)出包含主機(jī)名稱(chēng)、主機(jī) IP 以及新參加標(biāo)識(shí)符的播送說(shuō)明自己新參加;用戶(hù)列表:監(jiān)聽(tīng)到其他用戶(hù)的播送信息后更新列表,顯示當(dāng)前在線(xiàn)的用戶(hù)名和用戶(hù) IP;更新列表:在系統(tǒng)退出時(shí)會(huì)播送本機(jī)信息,標(biāo)識(shí)說(shuō)明自己離開(kāi),其余在線(xiàn)用戶(hù)接收到信息后更新提示該用戶(hù)下線(xiàn);選擇用戶(hù):可以單獨(dú)選擇或者多項(xiàng)選擇用戶(hù)列
25、表中的用戶(hù);專(zhuān)業(yè)資料整理WORD格式發(fā)送消息:選擇用戶(hù)后,在輸入框中輸入聊天信息,點(diǎn)擊“發(fā)送按鈕系統(tǒng)會(huì)發(fā)送 UDP 數(shù)據(jù)包給對(duì)應(yīng)用戶(hù)的ip 地址,端口是 8011;承受消息:系統(tǒng)實(shí)時(shí)監(jiān)聽(tīng) 8011 端口以承受不同用戶(hù)發(fā)來(lái)的消息并展示在聊天窗口中;發(fā)送文件:選擇指定用戶(hù),點(diǎn)擊“發(fā)送文件按鈕,選擇本機(jī)相應(yīng)文件后,與目標(biāo)用戶(hù)建立TCP 連接,發(fā)送字節(jié)流。端口為8011;承受文件:監(jiān)聽(tīng)到有TCP 連接請(qǐng)求時(shí)建立實(shí)時(shí)連接,系統(tǒng)提示用戶(hù)是否接收文件,選擇接收后選擇相應(yīng)路徑與文件名后進(jìn)展接收,接收成功后再聊天窗口顯示提示信息。4) 詳細(xì)設(shè)計(jì): 局域網(wǎng)用戶(hù)列表的建立:軟件啟動(dòng)后定時(shí)的使用 UDP 協(xié)議向255
26、.255.255.255這個(gè)播送地址發(fā)送播送包 ,端口設(shè)置為 8001。播送包內(nèi)容包含主機(jī)名、 IP、標(biāo)識(shí)符等信息,已啟動(dòng)軟件的用戶(hù)通過(guò) 8001 端口收到此播送包后,就會(huì)在自己的用戶(hù)列表中添加這個(gè)用戶(hù)的用戶(hù)名、 IP 地址等信息,從而局域網(wǎng)內(nèi)的本系統(tǒng)用戶(hù)都能建立起用戶(hù)列表;傳送與接收信息:傳送聊天信息時(shí)同樣使用 UDP 協(xié)議,用戶(hù)填寫(xiě)消息后點(diǎn)擊按鈕調(diào)用發(fā)送消息的函數(shù), 然后通過(guò) 8011 端口發(fā)送 UDP 包。系統(tǒng)在啟動(dòng)時(shí)通過(guò)開(kāi)辟新線(xiàn)程來(lái)監(jiān)聽(tīng) 8011 端口,時(shí)刻監(jiān)聽(tīng)發(fā)送到本機(jī)的信息。 發(fā)送文件:用戶(hù)發(fā)送文件時(shí)建立一個(gè)帶參數(shù)的線(xiàn)程來(lái)發(fā)送文件,調(diào)用相應(yīng)的 TCP 發(fā)送函數(shù)來(lái)發(fā)送文件,使用的是
27、8001 端口,由于協(xié)議不同因此與之前的播送并不沖突; 接收文件:軟件啟動(dòng)時(shí)開(kāi)啟一個(gè)線(xiàn)程用來(lái)接收文件,此線(xiàn)程在正常工作條件下處于阻塞監(jiān)聽(tīng)狀態(tài),直到收到建立 TCP 連接的請(qǐng)求,之后提示用戶(hù)收到文件選擇是否承受,之后選擇保存路徑,進(jìn)展接收。用戶(hù)離開(kāi):用戶(hù)離線(xiàn)時(shí)發(fā)送一個(gè)離線(xiàn)播送包到 55 的 8001 端口,包中含有 LEAVE 的標(biāo)識(shí)符,收到此播送包的用戶(hù), 根據(jù)包中的 IP 地址刪除用戶(hù)列表中相應(yīng)的用戶(hù)信息,并在聊天窗口顯示“ *X 用戶(hù)已經(jīng)離開(kāi); 聊天記錄保存:考慮到用戶(hù)有保存聊天記錄的需求,在聊天記錄右側(cè)點(diǎn)擊“保存按鈕,可以將當(dāng)前聊天記錄保存為 txt 文件到用戶(hù)
28、指定目錄中?;螯c(diǎn)專(zhuān)業(yè)資料整理WORD格式擊“清空,清空當(dāng)前用戶(hù)聊天記錄。5) 具體實(shí)現(xiàn): C#using System;using System.Collections.Generic;using SystemponentModel;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.Task
29、s;using System.Windows.Forms;namespace IPMessagepublic partial class frmMain : Form/用戶(hù)實(shí)體public class Userprivate string localname;/ 用戶(hù)名public string localNameget return localname; set localname = value; 專(zhuān)業(yè)資料整理WORD格式private string localip;/ 用戶(hù) IPpublic string localIPget return localip; set localip =
30、value; /用戶(hù)數(shù)組聲明public static List<User> Userslist = new List<User>();/獲得本機(jī)名和本機(jī)IPpublic User getInfo()User u = new User();string localName = Dns.GetHostName();/ 獲取主機(jī)名/Console.WriteLine(" 主機(jī)名: 0", localName);u.localName = localName;IPHostEntry localHost = Dns.GetHostEntry(localNam
31、e);/ 輸出對(duì)應(yīng)的 IP 地址IPAddress localIP = null;for (int i = 0; i < localHost.AddressList.Length; i+)if (localHost.AddressListi.AddressFamily = AddressFamily.InterNetwork)localIP = localHost.AddressListi;break;u.localIP = localIP.ToString();return u;專(zhuān)業(yè)資料整理WORD格式/發(fā)送播送消息public void broadMessage()Socketsoc
32、ket=newSocket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);IPEndPoint iep = new IPEndPoint(IPAddress.Parse("55"), 8001);/ 設(shè)置 Broadcast 值為表示允許套接字發(fā)送播送消息,該值默認(rèn)為不允許socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast,1);User u = getInfo();/ 標(biāo)
33、識(shí)符為 NEWstring send = "NEW"+","+u.localName + "," + u.localIP;byte sendM = System.Text.Encoding.Unicode.GetBytes(send);/byte sendIP = System.Text.Encoding.Unicode.GetBytes(u.localIP);while (isBroad)socket.SendTo(sendM, iep);Thread.Sleep(200);/socket.SendTo(sendIP, iep);s
34、ocket.Close();public void remove(User u)Userslist.Remove(u);this.rbChatContent.AppendText(" 用戶(hù): " + u.localName + " 離開(kāi) " + "n"); this.lbUser.Items.Remove(u.localName + "|" + u.localIP); ;專(zhuān)業(yè)資料整理WORD格式/接收播送消息public void reciveBroad()Socket socket = newSocket(Add
35、ressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);IPEndPoint iep = new IPEndPoint(IPAddress.Any, 8001);socket.Bind(iep);EndPoint ep = (EndPoint)iep;/System.Timers.Timer t = new System.Timers.Timer(1000); /t.Elapsed += new System.Timers.ElapsedEventHandler(theout); while (true)Byte getBytes
36、= new byte1024;socket.ReceiveFrom(getBytes, ref ep);string getData = System.Text.Encoding.Unicode.GetString(getBytes);getData = getData.TrimEnd('u0000');string sArray = getData.Split(',');/this.rbChatContent.AppendText(" 新上線(xiàn)用戶(hù):" + sArray1 + "n"); if (sArray.Length
37、 > 1)bool exist = false;if (Userslist!=null)foreach (User us in Userslist)if (us.localIP = sArray2)專(zhuān)業(yè)資料整理WORD格式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.A
38、ppendText(" 新上線(xiàn)用戶(hù):"+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(
39、" 用戶(hù): " + u.localName + " 離開(kāi)"+"n");this.lbUser.Items.Remove(u.localName + "|" + u.localIP);專(zhuān)業(yè)資料整理WORD格式Thread.Sleep(200);/ <summary>/ 通過(guò) TcpClient 滿(mǎn)足多用戶(hù)發(fā)送文件/ </summary>/ <param name="ip"></param>public void tcpSend(List<str
40、ing> 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, buff
41、er.Length);ns.Write(buffer, 0, size);len += size;專(zhuān)業(yè)資料整理WORD格式fs.Flush();ns.Flush();fs.Close();ns.Close();/ <summary>/ 使用 TcpClient 接收文件/ </summary>public void RecevieFile()TcpListener listener = new TcpListener(IPAddress.Any, 8001);listener.Start();while(true)tryint size = 0;int len = 0;
42、TcpClient client = listener.AcceptTcpClient();if (client.Connected)this.rbChatContent.AppendText(" 連接成功 ");NetworkStream stream = client.GetStream();專(zhuān)業(yè)資料整理WORD格式if (stream != null)DialogResult rs;專(zhuān)業(yè)資料整理WORD格式rs = MessageBox.Show(" 你收到一個(gè)文件",MessageBoxButtons.YesNo);if (rs = Dialog
43、Result.Yes), 是否接收?"," 提示專(zhuān)業(yè)資料整理WORD格式SaveFileDialog sfd = new SaveFileDialog();if(sfd.ShowDialog(this)=System.Windows.Forms.DialogResult.OK)string fileSavePath = sfd.FileName;/ 獲得用戶(hù)保存文件的路徑FileStreamfs=newFileStream(fileSavePath,FileMode.Create, FileAccess.Write);byte buffer = new byte1024;w
44、hile (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");專(zhuān)業(yè)資料整理WORD格式catch(Exception ex)MessageBox.Show(ex.ToString();/
45、<summary>/ 主函數(shù)/ </summary> 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(
46、this.listenMsg);rmThread.Start();專(zhuān)業(yè)資料整理WORD格式/離開(kāi)發(fā)送播送private void frmMain_FormClosing(object sender, FormClosingEventArgs e)isBroad = false;this.broadMessage();Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);IPEndPoint iep = new IPEndPoint(IPAddress.Parse(&q
47、uot;55"), 8001);/ 設(shè)置 Broadcast 值為表示允許套接字發(fā)送播送消息,該值默認(rèn)為不允許socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast,1);User u = getInfo();/ 標(biāo)識(shí)符為L(zhǎng)EAVEstring send = "LEAVE" + "," + u.localName + "," + u.localIP;byte sendM = System.Text.Encoding.Unicode.GetBytes(send);/byte sendIP = System.Text.Encoding.Unicode.GetBytes(u.localIP);socket.SendTo(sendM, iep);/socket.SendTo(sendIP, iep);socket.Close();System.Environment.Exit(0);/文件路徑public string filePath=null;private void btnSendFile_Click(object sender, EventArgs e)專(zhuān)業(yè)資料整理WORD格
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 深圳市二手房裝修工程施工合同
- 跨國(guó)(非獨(dú)占)品牌授權(quán)合作合同專(zhuān)業(yè)版
- 勞動(dòng)合同判例解析:合同糾紛與法律適用
- 實(shí)習(xí)生實(shí)習(xí)與就業(yè)合同書(shū)
- 反擔(dān)保責(zé)任合同模板
- 購(gòu)銷(xiāo)合同的反擔(dān)保書(shū)
- 全球商標(biāo)使用權(quán)轉(zhuǎn)讓合同
- 實(shí)習(xí)人員合同范本
- 終止建筑工程合同協(xié)議書(shū)
- 企業(yè)學(xué)徒工用工合同范本
- 開(kāi)學(xué)安全第一課主題班會(huì)課件
- 一年級(jí)珍惜糧食主題班會(huì)學(xué)習(xí)教案
- 新版《醫(yī)療器械經(jīng)營(yíng)質(zhì)量管理規(guī)范》(2024)培訓(xùn)試題及答案
- 2025年人教版數(shù)學(xué)五年級(jí)下冊(cè)教學(xué)計(jì)劃(含進(jìn)度表)
- 海岸動(dòng)力學(xué)英文課件Coastal Hydrodynamics-復(fù)習(xí)
- 碳足跡研究-洞察分析
- 硬質(zhì)巖層組合切割開(kāi)挖技術(shù)
- 2024解析:第二章聲現(xiàn)象-講核心(解析版)
- 2024年考研管理類(lèi)綜合能力(199)真題及解析完整版
- 2025年初級(jí)社會(huì)工作者綜合能力全國(guó)考試題庫(kù)(含答案)
- 2024解析:第十章 浮力綜合應(yīng)用-講核心(解析版)
評(píng)論
0/150
提交評(píng)論