




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、互聯(lián)網(wǎng)技術(shù)及應(yīng)用實踐實驗報告報告名稱: 路由器配置及應(yīng)用 路由追蹤的實現(xiàn) EMAIL的客戶端程序 開發(fā)一個簡單的WEB服務(wù)器 學(xué)生姓名: 學(xué) 號: 指導(dǎo)教師: 實驗一 路由器配置及應(yīng)用1.實驗原理2.實驗步驟 第一步:在路由器Router1上配置快速以太網(wǎng)口的IP地址Router1#configure terminal Router1(config)#interface FastEthernet 0/0 ! 進入以太網(wǎng)0口配置狀態(tài)或Router1(config)#interface FastEthernet 1/0 Router1(config-if)#ip address 172.16.1.
2、1 ! 為以太網(wǎng)0口配置地址Router1(config-if)#no shutdown ! 打開以太網(wǎng)0口Router1(config-if)#end 第二步:在路由器Router1上配置廣域網(wǎng)口的IP地址和時鐘頻率(假設(shè)Router1為DCE端)Router1#con Router1(config)#interface serial 2/0 ! 進入廣域網(wǎng)0口配置狀態(tài)或Router1(config)#interface serial 1/2 Router1(config-if)#ip address ! 為廣域網(wǎng)
3、0口配置地址Router1(config-if)#clock rate 64000 ! DCE端需設(shè)置端口時鐘頻率Router1(config-if)#no shutdown ! 打開廣域網(wǎng)0口Router1(config-if)#end 驗證測試:驗證路由器接口配置狀態(tài)。Router1#show ip interface brief ! 顯示IP端口狀態(tài)簡況Interface IP-Address OK? Method Status Protocol FastEthernet0/0 /24 YES manual up up FastEthernet0/1 no addre
4、ss YES unset administratively down down Serial2/0 /24 YES manual up up Serial3/0 no address YES unset administratively down down Null 0 no address YES up 第三步:為Router1添加靜態(tài)路由Router1#con Router1(config)#ip route ! 添加靜態(tài)路由Router1(config)#end Router1#show ip r
5、oute ! 顯示路由表Codes: C - connected, S - static, R - RIP O - OSPF, IA - OSPF inter area E1 - OSPF external type 1, E2 - OSPF external type 2 Gateway of last resort is not set /24 is subnetted, 3 subnets C is directly connected, FastEthernet0 C is directly connected, Seri
6、al0 S 1/0 via Router1#wr !保存所作的修改 第四步:在路由器Router2(R2624)上配置快速以太網(wǎng)口的IP地址Router2#con Router2(config)#interface fastethernet 0/0 或Router2(config)#interface fastethernet 1/0 Router2(config-if)#ip address Router2(config-if)#no shutdown Router2(config-if)#end
7、第五步:在路由器Router2(R2624)上配置廣域網(wǎng)口的IP地址Router2#conf Router2(config)#interface serial 2/0 或Router2(config)#interface serial 1/2 Router2(config-if)#ip address Router2(config-if)#no shutdown Router2(config-if)#end Router2#show ip interface brief Interface IP-Address OK? Method Statu
8、s Protocol FastEthernet0/0 YES manual up up FastEthernet0/1 unassigned YES unset administratively down down Serial2/0 YES manual up up Serial3/0 unassigned YES unset administratively down down 圖下圖:圖1.1第六步:測試兩臺路由器之間的連通性Router2#ping Type escape sequence to abort. Sendi
9、ng 5, 100-byte ICMP Echoes to , timeout is 2 seconds: !Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/4 ms 如下圖:圖1.2第七步:為Router2(R2624)添加靜態(tài)路由Router2#con Configuring from terminal, memory, or network terminal? Enter configuration commands, one per line. End with CNTL/Z. Rout
10、er2(config)#ip route Router2(config)#end Router2#show ip route Codes: C - connected, S - static, R - RIP O - OSPF, IA - OSPF inter area E1 - OSPF external type 1, E2 - OSPF external type 2 Gateway of last resort is not set /24 is subnetted, 3 subnets S 1
11、 1/0 via C is directly connected, Serial0 C is directly connected, FastEthernet0 Router2#wr 如下圖:圖1.3第八步:用ping命令對PC1,PC2進行連通性檢測如下圖:圖1.4注意事項: (1)如果兩臺路由器經(jīng)過串口直接相連,則必須在時鐘一端設(shè)置時鐘頻率(DCE)。 (2)PC機必須配置默認網(wǎng)關(guān)。實驗二 路由追蹤1. 實驗?zāi)康模菏煜ふ莆章酚善鲗崿F(xiàn)路由追蹤,熟悉ICMP協(xié)議2. 實驗環(huán)境:Microsoft Visual
12、C+6.03.實驗原理:路由追蹤是從源主機向目的主機發(fā)送一連串的IP數(shù)據(jù)報,數(shù)據(jù)包中封裝的是無法交付的UDP數(shù)據(jù)報。源主機先向目的主機發(fā)送一個回應(yīng)請求報文(類型8),TTL設(shè)置為1,第一個路由器收到后 將TTL設(shè)為1第一個路由器收到后將TTL減1,這樣TTL變?yōu)?,分組被廢除,同時路由器向源主機發(fā)送一個TTL超時報文(類型為11),報文的IP報頭中的源IP地址就是第一個路由器的地址,源主機可以通過對該報文進行分析,得到第一個路由器的地址,接著發(fā)送TTL等于2的報文得到第二個路由器的地址,再發(fā)送TTL等于3的報文,如此下去直到收到目的主機的回應(yīng)應(yīng)答報文(類型為0)或不可到達報文(類型為3),或者
13、到了最大跳數(shù)(要檢測路由器個數(shù)的最大值)。這樣,源主機達到了自己的目的,因為這些路由器和最后的目的主機發(fā)送的ICMP報文正好給源主機想知道的路由信息到達目的主機所經(jīng)過的路由器的IP地址,以及到達其中每一個路由器的往返時間。4.源代碼如下:#define WIN32_LEAN_AND_MEAN #include <winsock2.h> /使用原始套接字需要WinSock2的支持 #include <ws2tcpip.h> /進行IPPROTO_IP級別設(shè)置時用到 #include <stdio.h> #include <stdlib.h> #de
14、fine ICMP_ECHO 8 /發(fā)送Ping請求時的ICMP報文類型 #define ICMP_ECHOREPLY 0 /接收Ping回復(fù)時的ICMP報文類型 #define ICMP_TIMEOUT 11 /ICMP超時報文類型 #define ICMP_MIN 8 /Minimum 8-byte ICMP packet (header) #define MAX_PACKET 1024 /Max ICMP packet size #define DEICMP_PACKSIZE 44 /Defaut ICMP PACKET SIZE char lpdest16; /用來存放目的IP地址 D
15、WORD cStartTickCount; /用來存放發(fā)送包的起始時間 #pragma comment( lib, "ws2_32.lib" ) typedef struct _icmphdr /ICMP頭部定義,被封裝在IP包中 BYTE i_type; /報文類型 BYTE i_code; /代碼 USHORT i_cksum; /校驗和 USHORT i_id; /標(biāo)識符 USHORT i_seq; /序號 IcmpHeader; /初始化ICMP頭部 void FillICMPData(char *icmp_data,int datasize) IcmpHeader
16、 *icmp_hdr=NULL; char *datapart=NULL; icmp_hdr=(IcmpHeader *)icmp_data; icmp_hdr->i_type=ICMP_ECHO; /request an ICMP echo icmp_hdr->i_code=0; icmp_hdr->i_id=(USHORT)GetCurrentProcessId(); icmp_hdr->i_cksum=0; icmp_hdr->i_seq=0; datapart=icmp_data+sizeof(IcmpHeader); memset(datapart,
17、39;E',datasize-sizeof(IcmpHeader); /校驗和函數(shù) USHORT checksum(USHORT *buffer,int size) unsigned long cksum=0; while(size>1) cksum+=*buffer+; size-=sizeof(USHORT); if(size) cksum+=*(UCHAR *)buffer; cksum=(cksum>>16)+(cksum & 0xffff); cksum+=(cksum>>16); return (USHORT)(cksum); int
18、DecodeIPHeader(char *buf,int bytes,struct sockaddr_in *from) IcmpHeader *icmphdr=NULL; DWORD tick; static int icmpcount=1; unsigned short iphdrlen; /判斷接收操作是否超時 if(!buf) printf("%2d: *.*.*.* Request timed out.n",icmpcount+); return 1; tick=GetTickCount(); iphdrlen=(buf0 & 0x0f)*4; icmph
19、dr=(IcmpHeader *)(buf+iphdrlen); if(bytes<iphdrlen+ICMP_MIN) printf("Too few bytes from %sn",inet_ntoa(from->sin_addr); return 0; /判斷接收的ICMP報文是否為超時報文 if(icmphdr->i_type=ICMP_TIMEOUT&&icmphdr->i_code=0) printf("%2d: %-15s %4dmsn",icmpcount+,inet_ntoa(from->s
20、in_addr),tick-cStartTickCount); return 0; /判斷接收的ICMP報文是否為回復(fù)報文 else if(icmphdr->i_type=ICMP_ECHOREPLY&&icmphdr->i_id=GetCurrentProcessId() printf("%2d: %-15s %4dmsn",icmpcount+,inet_ntoa(from->sin_addr),tick-cStartTickCount); printf("Trace complete!n"); return 1;
21、/其他類型,表示不可達 else printf("%2d: Destination host is unreachable!n",icmpcount+); return 1; int main() WSADATA wsaData; SOCKET sockRaw=INVALID_SOCKET; struct sockaddr_in dest, from; int i,bread,fromlen=sizeof(from),timeout=1000,ret; struct hostent *hp=NULL; char *icmp_data=NULL,*recvbuf=NULL;
22、USHORT seq_no=0; printf("Destination Address(IP/Host name):"); scanf("%s",lpdest); if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0) printf("WSAStartup() failed:%dn",GetLastError(); return -1; /創(chuàng)建套接字 sockRaw=WSASocket(AF_INET,SOCK_RAW,IPPROTO_ICMP,NULL,0,WSA_FLAG_OVERLAPPED
23、); if(sockRaw=INVALID_SOCKET) printf("WSASocket() failed:%dn",WSAGetLastError(); return -1; /對鎖定套接字設(shè)置超時 bread=setsockopt(sockRaw,SOL_SOCKET,SO_RCVTIMEO,(char *)&timeout,sizeof(timeout); if(bread=SOCKET_ERROR) printf("setsockopt(SO_RCVTIMEO) failed:%dn",WSAGetLastError(); retu
24、rn -1; timeout=1000; bread=setsockopt(sockRaw,SOL_SOCKET,SO_SNDTIMEO,(char *)&timeout,sizeof(timeout); if(bread=SOCKET_ERROR) printf("setsockopt(SO_SNDTIMEO) failed:%dn",WSAGetLastError(); return -1; /解析目標(biāo)地址,將主機名轉(zhuǎn)化為IP地址 memset(&dest,0,sizeof(dest); dest.sin_family=AF_INET; if(dest.
25、sin_addr.S_un.S_addr=inet_addr(lpdest)=INADDR_NONE) if(hp=gethostbyname(lpdest)!=NULL) memcpy(&(dest.sin_addr.S_un.S_addr),hp->h_addr_list0,hp->h_length); dest.sin_family=hp->h_addrtype; printf("dest.sin_addr=%sn",inet_ntoa(dest.sin_addr); else printf("gethostbyname() fai
26、led:%dn",WSAGetLastError(); return -1; /Create the ICMP pakcet icmp_data= (char*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,MAX_PACKET); recvbuf = (char*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,MAX_PACKET); if(!icmp_data) printf("HeapAlloc() failed: %dn",GetLastError(); return -1
27、; memset(icmp_data,0,MAX_PACKET); FillICMPData(icmp_data,DEICMP_PACKSIZE); printf("Hop IP Address Time elapsedn"); /開始發(fā)送/接收ICMP報文 for(i=1;i<=255;i+) int bwrote; /設(shè)置IP包的生存期 ret=setsockopt(sockRaw,IPPROTO_IP,IP_TTL,(char *)&i,sizeof(int); if(ret=SOCKET_ERROR) printf("setsockopt(I
28、P_TTL) failed:%dn",WSAGetLastError(); (IcmpHeader *)icmp_data)->i_cksum =0; (IcmpHeader *)icmp_data)->i_seq=seq_no+; /Sequence number of ICMP packets (IcmpHeader *)icmp_data)->i_cksum=checksum(USHORT *)icmp_data,DEICMP_PACKSIZE); /發(fā)送ICMP包請求查詢 cStartTickCount=GetTickCount(); bwrote=send
29、to(sockRaw,icmp_data,DEICMP_PACKSIZE,0,(struct sockaddr *)&dest,sizeof(dest); if(bwrote=SOCKET_ERROR) if(WSAGetLastError()=WSAETIMEDOUT) printf("timed outn"); continue; printf("sendto() failed:%dn",WSAGetLastError(); return -1; if(bwrote<DEICMP_PACKSIZE) printf("Wrote
30、 %d bytesn",bwrote); /接收ICMP回復(fù)包 bread=recvfrom(sockRaw,recvbuf,MAX_PACKET,0,(struct sockaddr *)&from,&fromlen); if(bread=SOCKET_ERROR) if(WSAGetLastError()=WSAETIMEDOUT) DecodeIPHeader(NULL,0,NULL); continue; printf("recvfrom() failed:%dn",WSAGetLastError(); return -1; if(Deco
31、deIPHeader(recvbuf,bread,&from) break; Sleep(1000); system("pause"); if(sockRaw!=INVALID_SOCKET) closesocket(sockRaw); HeapFree(GetProcessHeap(),0,recvbuf); HeapFree(GetProcessHeap(),0,icmp_data); WSACleanup(); return 0; 5.實驗結(jié)果如圖:實驗三 EMAIL的客戶端程序1.代碼如下:using System;using System.Collecti
32、ons.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Sh
33、apes;using System.Net.Mail;using System.Net.Sockets;using System.IO;namespace mail / <summary> / MainWindow.xaml 的交互邏輯 / </summary> public partial class MainWindow : Window TcpClient server;/接服務(wù)器 string sendstring;/用于存儲POP3服務(wù)命令參數(shù) byte bufferstring;/用于存儲POP3服務(wù)命令參數(shù)字節(jié)數(shù) NetworkStream network
34、stream;/接服務(wù)器與服務(wù)器進行數(shù)據(jù)交互 StreamReader streamreader;/讀取信息數(shù)據(jù) public MainWindow() InitializeComponent(); private void sendMail_Click(object sender, RoutedEventArgs e) /SmtpClient client = new SmtpClient(""); SmtpClient client = new SmtpClient(""); client.Port = 25; MailMessage mail =
35、new MailMessage(); mail.From = new MailAddress("liuweituan1989", "我是1234");/加上自定義的發(fā)件人顯示名稱 mail.To.Add(new MailAddress("120172167"); mail.Subject = "Test SmtpClient" mail.Body = "This message is for testing." client.DeliveryMethod = SmtpDeliveryMethod
36、.Network; client.UseDefaultCredentials = true; /啟用身份認證 mail.Priority = MailPriority.Normal; /郵件優(yōu)先級 /mail.Attachments.Add(new Attachment("F:我的文檔消息.txt"); /附件 /mail.Body = new System.IO.StreamReader("F:我的文檔ttt.txt", Encoding.Default).ReadToEnd(); 從文本文件中讀取郵件內(nèi)容 mail.BodyEncoding = Sy
37、stem.Text.Encoding.UTF8; client.Credentials = new System.Net.NetworkCredential("liuweituan1989", "19891217421X");/如果是匿名發(fā)送則不需要這一句(這里將賬號和密碼用x和*代替了) / client.Send(mail); client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);/回調(diào)函數(shù) string userState = "測試&qu
38、ot; client.SendAsync(mail, userState); private static void SendCompletedCallback(object sender, System.ComponentModel.AsyncCompletedEventArgs e) if (e.Cancelled) /郵件發(fā)送被取消 if (e.Error != null) /郵件發(fā)送失敗 else /發(fā)送成功 private void Showinfo() richTextBox1.Document.Blocks.Clear(); try string strResult = &quo
39、t;" sendstring = "RETR " + this.textBox4.Text + "rn"/存儲從服務(wù)器獲得一條信息的命令 bufferstring = Encoding.ASCII.GetBytes(sendstring.ToCharArray(); networkstream.Write(bufferstring, 0, bufferstring.Length); strResult = streamreader.ReadLine(); if (strResult0 != '-') /不斷地讀取郵件內(nèi)容,只到結(jié)
40、束標(biāo)志:英文句號 while (strResult != ".") this.richTextBox1.AppendText(strResult + "rn"); strResult = streamreader.ReadLine(); else this.richTextBox1.AppendText("rn" + "郵件錯誤" + "rn"); catch (Exception ey) MessageBox.Show(ey.Message); private void getMail_Cli
41、ck(object sender, RoutedEventArgs e) Showinfo(); private void login_Click(object sender, RoutedEventArgs e) server = new TcpClient(this.textBox1.Text, 110);/實例TcpClient 類對象聯(lián)接服務(wù)器 networkstream = server.GetStream();/實例NetworkStream類對象接收返回發(fā)送的數(shù)據(jù) streamreader = new StreamReader(networkstream);/實例StreamRe
42、ader類對象讀取數(shù)據(jù) try sendstring = "USER " + this.textBox2.Text + "rn"/存儲用戶名 bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray(); networkstream.Write(bufferstring, 0, bufferstring.Length);/將用戶名發(fā)送到服務(wù)器 richTextBox1.AppendText(streamreader.ReadLine()
43、+ "rn");/將用用戶顯示在 richTextBox控件中 sendstring = "PASS " + this.textBox3.Text + "rn"/存儲用戶密碼 bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray(); networkstream.Write(bufferstring, 0, bufferstring.Length);/將用戶密碼發(fā)送到服務(wù)器 richTextBox1.AppendT
44、ext(streamreader.ReadLine() + "rn"); sendstring = "STAT " + "rn"/儲存從服務(wù)器獲得所有信息序號和字節(jié)數(shù)命令 bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray(); networkstream.Write(bufferstring, 0, bufferstring.Length);/從服務(wù)器獲得所有信息序號和字節(jié)數(shù) string strResult
45、= streamreader.ReadLine();/讀取從服務(wù)器返回的數(shù)據(jù) if (strResult.IndexOf('-') = -1) richTextBox1.AppendText(strResult + "rn"); sendstring = "LIST " + "rn"/存儲從服務(wù)器中獲得信息列表和大小的命令 bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray(); networks
46、tream.Write(bufferstring, 0, bufferstring.Length); string strInfo = streamreader.ReadLine(); string str = strInfo.Split(' '); richTextBox1.AppendText("郵件數(shù)量:" + str1 + "rn"); richTextBox1.AppendText(str1 + ":封郵件總?cè)萘繛?quot; + str2 + "rn"); MessageBox.Show(this.textBox2.Text + "用戶您好!"); else MessageBox.Show("讀取信息有誤,請重新登錄"); catch (Exception ey) MessageBox.Show(ey.Message); 2.實驗結(jié)果:實驗四 簡單的WEB服務(wù)器開發(fā)1. 實驗?zāi)?/p>
溫馨提示
- 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)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 第2.6講 指數(shù)與指數(shù)函數(shù)(原卷版)-2024年高考數(shù)學(xué)一輪復(fù)習(xí)精講精練寶典(新高考專用)
- 菏澤黃底真石漆施工方案
- 第三單元課外古詩詞教學(xué)設(shè)計 2024-2025學(xué)年七年級上冊同步
- 博物館拆遷合同范本
- 優(yōu)化公司供應(yīng)商管理的工作計劃
- 構(gòu)建班級學(xué)習(xí)共同體的工作策略計劃
- 老年肺炎區(qū)別護理
- 公司生產(chǎn)工作計劃改善生產(chǎn)線平衡性
- 農(nóng)業(yè)發(fā)展安全服務(wù)保障藍圖計劃
- 小班集體游戲的安排與實施計劃
- 對外漢語初級水果教學(xué)課件
- 電工技術(shù)基礎(chǔ)與技能教案(第3版)
- 二年級《書聲朗朗》
- 有效溝通技巧課件
- 3.《東施效顰》課件PPT
- 幼兒園小班語言《娃娃開門》課件
- 梯籠安裝施工方法
- JIS G4304-2021 熱軋不銹鋼板材、薄板材和帶材
- 浮針療法之頸椎病的治療ppt課件
- 五防、后臺調(diào)試報告
- VISIO圖標(biāo)大全(完整版)
評論
0/150
提交評論