实例介绍
【实例简介】
【实例截图】
【核心代码】
using System; using System.Collections.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.Shapes; using System.Net; using System.Net.Sockets; namespace Tracert { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public class IcmpPacket { private Byte _type; private Byte _subCode; private UInt16 _checkSum; private UInt16 _identifier; private UInt16 _sequenceNumber; private Byte[] _data; public IcmpPacket(Byte type, Byte subCode, UInt16 checkSum, UInt16 identifier, UInt16 sequenceNumber, int dataSize) { _type = type; _subCode = subCode; _checkSum = checkSum; _identifier = identifier; _sequenceNumber = sequenceNumber; _data = new Byte[dataSize]; for (int i = 0; i < dataSize; i ) { _data[i] = (byte)'#'; } } public UInt16 CheckSum { get { return _checkSum; } set { _checkSum = value; } } //初始化ICMP报文 public int CountByte(Byte[] buffer) { Byte[] b_type = new Byte[1] { _type }; Byte[] b_code = new Byte[1] { _subCode }; Byte[] b_cksum = BitConverter.GetBytes(_checkSum); Byte[] b_id = BitConverter.GetBytes(_identifier); Byte[] b_seq = BitConverter.GetBytes(_sequenceNumber); int i = 0; Array.Copy(b_type, 0, buffer, i, b_type.Length); i = b_type.Length; Array.Copy(b_code, 0, buffer, i, b_code.Length); i = b_code.Length; Array.Copy(b_cksum, 0, buffer, i, b_cksum.Length); i = b_cksum.Length; Array.Copy(b_id, 0, buffer, i, b_id.Length); i = b_id.Length; Array.Copy(b_seq, 0, buffer, i, b_seq.Length); i = b_seq.Length; Array.Copy(_data, 0, buffer, i, _data.Length); i = _data.Length; return i; } //将ICMP报文转化为Byte数据包 public static UInt16 SumOfCheck(UInt16[] buffer) { int cksum = 0; for (int i = 0; i < buffer.Length; i ) { cksum = (int)buffer[i]; } cksum = (cksum >> 16) (cksum & 0xffff); cksum = (cksum >> 16); return (UInt16)(~cksum); } } private void button1_Click(object sender, RoutedEventArgs e) { listBox1.Items.Clear(); try { if (textBox1.Text.Length == 0) { listBox1.Items.Add("请键入IP地址"); textBox1.Focus(); return; } Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); //创建接收和发送ICMP报文的Socket IPEndPoint ipDEST = new IPEndPoint(Dns.Resolve(textBox1.Text).AddressList[0], 30); //设定服务器端30端口号对应的网络节点 IPEndPoint ipSRC = new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], 30); EndPoint epSRC = (EndPoint)ipSRC; int DataSize = 4;//ICMP报文中的数据长度 int PacketSize = DataSize 8;//报文总长度 const int ICMP_ECHO = 8; IcmpPacket packet = new IcmpPacket(ICMP_ECHO, 0, 0, 45, 0, DataSize); Byte[] Buffer = new Byte[PacketSize]; int index = packet.CountByte(Buffer); if (index != PacketSize) { listBox1.Items.Add("报文有问题"); return; } int cksum_buffer_length = (int)Math.Ceiling(((Double)index) / 2); UInt16[] cksum_buffer = new UInt16[cksum_buffer_length]; int icmp_header_buffer_index = 0; for (int i = 0; i < cksum_buffer_length; i ) { cksum_buffer[i] = BitConverter.ToUInt16(Buffer, icmp_header_buffer_index); icmp_header_buffer_index = 2; } //将校验和保存在报文中 packet.CheckSum = IcmpPacket.SumOfCheck(cksum_buffer); //保存校验和后,再次将报文转化为数据包 Byte[] ByteSend = new Byte[PacketSize]; index = packet.CountByte(ByteSend); if (index != PacketSize) { listBox1.Items.Add("报文有问题"); return; } //发送不同TTL值的数据包 for (int ittl = 1; ittl <= 255; ittl ) { Byte[] ByteRecv = new Byte[256]; s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, ittl); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 10000); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000); DateTime dt = DateTime.Now; int iRet = s.SendTo(ByteSend, ByteSend.Length, SocketFlags.None, ipDEST); //发送ICMP报文 if (iRet == -1) { listBox1.Items.Add("发送数据出错"); } iRet = s.ReceiveFrom(ByteRecv, ByteRecv.Length, SocketFlags.None, ref epSRC); TimeSpan ts = DateTime.Now - dt; //检测回应是否正确 if (iRet == -1) { listBox1.Items.Add("接收数据出错"); } listBox1.Items.Add("TTL=" ittl.ToString() "IP=" ((IPEndPoint)epSRC).Address.ToString() "Time=" ts.Milliseconds.ToString()); //判断是否到达目标地址 if (Dns.GetHostEntry(textBox1.Text).AddressList[0].ToString() == ((IPEndPoint)epSRC).Address.ToString()) { break; } //超时错误 if (ByteRecv[20] != 11) { listBox1.Items.Add("超时,请重试"); break; } } } catch (SocketException b) { MessageBox.Show(b.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论