扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:阿虎 来源:天极开发 2007年11月11日
关键字:
Private Void Button1_click ( Object Sender , System.eventargs E ) { Listbox1.items.clear ( ) ; String Hostclient = Textbox1.text ; Int K ; For ( K = 0 ; K < 3 ; K++ ) { Socket Socket = New Socket ( Addressfamily.internetwork , Sockettype.raw , Protocoltype.icmp ) ; Iphostentry Hostinfo ; Try { //解析主机ip入口 Hostinfo = Dns.gethostbyname ( Hostclient ) ; } Catch ( Exception ) { //解析主机名错误。 Listbox1.items.add ( "没有发现此主机!" ) ; Return ; } // 取服务器端主机的30号端口 Endpoint Hostpoint = ( Endpoint ) New Ipendpoint ( Hostinfo.addresslist[ 0 ] , 30 ) ; Iphostentry Clientinfo ; Clientinfo = Dns.gethostbyname ( Hostclient ) ; // 取客户机端主机的30端口 Endpoint Clientpoint = ( Endpoint ) New Ipendpoint ( Clientinfo.addresslist[ 0 ] , 30 ) ; //设置icmp报文 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++ ) { //将两个byte转化为一个uint16 Cksum_buffer[ I ] = Bitconverter.touint16 ( Buffer , Icmp_header_buffer_index ) ; Icmp_header_buffer_index += 2 ; } //将校验和保存至报文里 Packet.checksum = Icmppacket.sumofcheck ( Cksum_buffer ) ; // 保存校验和后,再次将报文转化为数据包 Byte [ ] Senddata = New Byte [ Packetsize ] ; Index = Packet.countbyte ( Senddata ) ; //报文出错 If ( Index != Packetsize ) { Listbox1.items.add ( "报文出现问题!" ) ; Return ; } Int Nbytes = 0 ; //系统计时开始 Int Starttime = Environment.tickcount ; //发送数据包 If ( ( Nbytes = Socket.sendto ( Senddata , Packetsize , Socketflags.none , Hostpoint ) ) == -1 ) { Listbox1.items.add ( "无法传送报文!" ) ; } Byte [ ] Receivedata = New Byte[ 256 ] ; //接收数据 Nbytes = 0 ; Int Timeout = 0 ; Int Timeconsume = 0 ; While ( True ) { Nbytes = Socket.receivefrom ( Receivedata , 256 , Socketflags.none , Ref Clientpoint ) ; If ( Nbytes == -1 ) { Listbox1.items.add ( "主机没有响应!" ) ; Break ; } Else If ( Nbytes > 0 ) { Timeconsume = System.environment.tickcount - Starttime ; //得到发送报文到接收报文之间花费的时间 Listbox1.items.add ( "reply From " + Hostinfo.addresslist[ 0 ].tostring ( ) + " In " + Timeconsume + "ms :bytes Received " + Nbytes ) ; Break ; } Timeconsume = Environment.tickcount - Starttime ; If ( Timeout > 1000 ) { Listbox1.items.add ( "time Out" ) ; Break ; } } //关闭套接字 Socket.close ( ) ; } } |
{ 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 ) ; } } |
图06:【Visual C#实现Ping命令】的运行界面 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者